944,124 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 593
  • MS SQL RSS
Oct 28th, 2009
0

creating headr name dynamically

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shangita is offline Offline
1 posts
since Oct 2009
Nov 5th, 2009
0
Re: creating headr name dynamically
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:
MS SQL Syntax (Toggle Plain Text)
  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
Reputation Points: 14
Solved Threads: 12
Junior Poster
kplcjl is offline Offline
146 posts
since Sep 2009
Nov 5th, 2009
0
Re: creating headr name dynamically
Whoops when I modified the last select
MS SQL Syntax (Toggle Plain Text)
  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
Reputation Points: 14
Solved Threads: 12
Junior Poster
kplcjl is offline Offline
146 posts
since Sep 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Stored Procedure with Increments
Next Thread in MS SQL Forum Timeline: strong sa password error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC