creating headr name dynamically

Reply

Join Date: Oct 2009
Posts: 1
Reputation: shangita is an unknown quantity at this point 
Solved Threads: 0
shangita shangita is offline Offline
Newbie Poster

creating headr name dynamically

 
0
  #1
Oct 28th, 2009
I created 2 tables

1 st table as csat
csat_code csat_ou_cod csat_csaeid cus name
CSA00001 ADM00001 6825 CUS08349
CSA00002 ADM00001 6826 CUS08347
CSA00003 ADM00001 6832 CUS08351
CSA02547 ADM00001 6824 CUS04150


table2 as csat_survey

surveycode csat_code qns id ans id
SUR00001 CSA02547 1 3
SUR00002 CSA02547 4 7


now i want the resut is


csat_code csat_case_id qns id(column name)


like the row field of qns id ( table2) is cloumn for my solution table. and the number of qns also varied .so i have to create the dynamic header.. plz any one help me? how can i get this solution?


Thanks in advance

Sangita.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 42
Reputation: kplcjl is an unknown quantity at this point 
Solved Threads: 5
kplcjl kplcjl is offline Offline
Light Poster
 
0
  #2
Nov 5th, 2009
1. Your format is almost unreadable. I suggest putting commas between column names and if your column name has a space, bracket it like <cus name>
2. You can't have a dynamic number of columns in a single result set
3. What you want isn't none too clear either.
4. if you want data combined from several rows into one row something like this:
CSA02547 S=SUR00001,Q=1,A=3;S=SUR00002,Q=4,A=7
CUS08347 S=SUR00001,Q=1,A=4;S=SUR00002,Q=3,A=1;S=SUR00003,Q=7,A=6
here is the code that will produce that result:
  1. --Initialize two tables
  2. declare @tbl1 TABLE(c_code varchar(20), ou_code varchar(20), id int NULL,CName varchar(20))
  3. INSERT INTO @tbl1
  4. SELECT 'CSA00001', 'ADM00001', 6825, 'CUS08349'
  5. union SELECT 'CSA00002', 'ADM00001', 6826, 'CUS08347'
  6. union SELECT 'CSA00003', 'ADM00001', 6832, 'CUS08351'
  7. union SELECT 'CSA02547', 'ADM00001', 6824, 'CUS04150'
  8. declare @tbl2 TABLE(surveycode varchar(20), c_code varchar(20), Qns_id int ,Ans_id int)
  9. INSERT INTO @tbl2
  10. SELECT 'SUR00001', 'CSA02547', 1, 3
  11. union SELECT 'SUR00001', 'CUS08347', 1, 4
  12. union SELECT 'SUR00002', 'CUS08347', 3, 1
  13. union SELECT 'SUR00003', 'CUS08347', 7, 6
  14. union SELECT 'SUR00002', 'CSA02547', 4, 7
  15.  
  16. -- calculate cumulative total
  17. declare @tbl3 TABLE(id int identity PRIMARY KEY,
  18. surveycode varchar(20), c_code varchar(20), Qns_id varchar(20),
  19. Ans_id varchar(20),Sum_id int NULL, S_Ans varchar(max) NULL)
  20. INSERT INTO @tbl3 (surveycode, c_code, Qns_id, Ans_id) SELECT * FROM @tbl2 ORDER BY c_code,surveycode
  21. UPDATE @tbl3 SET S_Ans='S=' + surveycode + ',Q=' + Qns_id + ',A=' + Ans_id
  22. UPDATE @tbl3 SET Sum_id=id WHERE id IN (SELECT min(id) FROM @tbl3 GROUP BY c_code)
  23. while @@rowcount > 0
  24. UPDATE a SET S_Ans=a.S_Ans + ';' + b.S_Ans, Sum_id=a.Sum_id+1
  25. FROM @tbl3 a JOIN @tbl3 b ON b.id=a.Sum_id+1 AND a.c_code=b.c_code
  26. SELECT c_code,S_Ans FROM @tbl3 WHERE Sum_id>0
  27. ORDER BY c_code
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 42
Reputation: kplcjl is an unknown quantity at this point 
Solved Threads: 5
kplcjl kplcjl is offline Offline
Light Poster
 
0
  #3
Nov 5th, 2009
Whoops when I modified the last select
  1. DELETE FROM @tbl3 WHERE Sum_id IS NULL
  2. SELECT a.*,S_Ans FROM @tbl1 a LEFT JOIN @tbl3 b ON a.c_code=b.c_code
  3. ORDER BY c_code
for results
CSA00001 ADM00001 6825 CUS08349 NULL
CSA00002 ADM00001 6826 CUS08347 S=SUR00001,Q=1,A=4;S=SUR00002,Q=3,A=1;S=SUR00003,Q=7,A=6
CSA00003 ADM00001 6832 CUS08351 NULL
CSA02547 ADM00001 6824 CUS04150 S=SUR00001,Q=1,A=3;S=SUR00002,Q=4,A=7
I realized I'd typo'd the column value and had to correct the wrong value to be CSA00002
Last edited by kplcjl; Nov 5th, 2009 at 12:47 am. Reason: Clarify one statement
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 447 | Replies: 2
Thread Tools Search this Thread



Tag cloud for MS SQL
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC