| Author |
Topic |
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2008-02-05 : 04:07:45
|
hi,i'm having following data:create table survey(clientID int not null,questionID int not null,Answer int not null) insert into survey (clientID, questionID, Answer) values (1, 1, 1)insert into survey (clientID, questionID, Answer) values (1, 1, 2)insert into survey (clientID, questionID, Answer) values (1, 1, 4)insert into survey (clientID, questionID, Answer) values (1, 1, 5)insert into survey (clientID, questionID, Answer) values (2, 1, 1)insert into survey (clientID, questionID, Answer) values (2, 1, 5)insert into survey (clientID, questionID, Answer) values (2, 1, 8)insert into survey (clientID, questionID, Answer) values (3, 1, 2)insert into survey (clientID, questionID, Answer) values (3, 1, 4)insert into survey (clientID, questionID, Answer) values (3, 1, 6)insert into survey (clientID, questionID, Answer) values (3, 1, 9)insert into survey (clientID, questionID, Answer) values (4, 1, 8)insert into survey (clientID, questionID, Answer) values (5, 1, 1)insert into survey (clientID, questionID, Answer) values (5, 1, 5)insert into survey (clientID, questionID, Answer) values (5, 1, 9) and i want to get it out like this:ClientID Q1_1 Q1_2 Q1_3 Q1_4----------------------------------------1 |1 |2 |4 |5 |2 |1 |5 |8 |0 |3 |2 |4 |6 |9 |4 |8 |0 |0 |0 |5 |1 |5 |9 |0 |but i get the following output:clientID Q1_1 Q1_2 Q1_3 Q1_4----------- ----------- ----------- ----------- -----------1 12 12 12 122 14 14 14 143 21 21 21 214 8 8 8 85 15 15 15 15(5 row(s) affected)with this code:SELECT distinct(clientID) ,Q1_1 ,Q1_2 ,Q1_3 ,Q1_4from (select distinct(clientID) ,sum(case when QuestionID = 1 and Answer in (1,2,3,4,5,6,7,8,9,10) then Answer else 0 end) as Q1_1 ,sum(case when QuestionID = 1 and Answer in (1,2,3,4,5,6,7,8,9,10) then Answer else 0 end) as Q1_2 ,sum(case when QuestionID = 1 and Answer in (1,2,3,4,5,6,7,8,9,10) then Answer else 0 end) as Q1_3 ,sum(case when QuestionID = 1 and Answer in (1,2,3,4,5,6,7,8,9,10) then Answer else 0 end) as Q1_4 from dbo.survey group by clientid) as xgroup by x.clientID ,x.Q1_1 ,x.Q1_2 ,x.Q1_3 ,x.Q1_4order by clientID asc what am i doing wrong? Thank you |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-05 : 04:20:32
|
use this:-;With Your_CTE (RowNo,ClientID,QuestionID,Answer) AS(SELECT ROW_NUMBER() OVER (PARTITION BY ClientID ORDER BY Answer),clientID, questionID, AnswerFROM survey)SELECT t1.ClientID,ISNULL(t2.Answer,0) AS Q_1_1,ISNULL(t3.Answer,0)AS Q_1_2,ISNULL(t4.Answer,0)AS Q_1_3,ISNULL(t5.Answer,0)AS Q_1_4FROM (SELECT DISTINCT ClientID FROM Your_CTE) t1OUTER APPLY (SELECT Answer FROM Your_CTE WHERE RowNo=1 AND ClientID=t1.ClientID)t2OUTER APPLY (SELECT Answer FROM Your_CTE WHERE RowNo=2 AND ClientID=t1.ClientID)t3OUTER APPLY (SELECT Answer FROM Your_CTE WHERE RowNo=3 AND ClientID=t1.ClientID)t4OUTER APPLY (SELECT Answer FROM Your_CTE WHERE RowNo=4 AND ClientID=t1.ClientID)t5 |
 |
|
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2008-02-05 : 04:22:16
|
| Thanks, is there anything that works on SQL server 2000? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-05 : 04:34:43
|
sql 2000 compatible soln:-DECLARE @temp table(RowNo int,clientID int,questionID int,Answer int)INSERT INTO @temp (RowNo,clientID, questionID, Answer)SELECT (SELECT COUNT(*) + 1 FROM survey WHERE clientId=s.clientId and Answer < s.Answer),clientID, questionID, AnswerFROM survey sORDER BY clientID,AnswerSELECT t1.clientID, ISNULL(t2.Answer,0) AS Q_1_1,ISNULL(t3.Answer,0)AS Q_1_2,ISNULL(t4.Answer,0)AS Q_1_3,ISNULL(t5.Answer,0)AS Q_1_4 FROM (SELECT DISTINCT clientID FROM @temp) t1LEFT OUTER JOIN @temp t2ON t2.clientID=t1.clientIDAND t2.RowNo=1LEFT OUTER JOIN @temp t3ON t3.clientID=t1.clientIDAND t3.RowNo=2LEFT OUTER JOIN @temp t4ON t4.clientID=t1.clientIDAND t4.RowNo=3LEFT OUTER JOIN @temp t5ON t5.clientID=t1.clientIDAND t5.RowNo=4 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-05 : 04:46:54
|
even simpler (removed all joins)DECLARE @temp table(RowNo int,clientID int,questionID int,Answer int)INSERT INTO @temp (RowNo,clientID, questionID, Answer)SELECT (SELECT COUNT(*) + 1 FROM survey WHERE clientId=s.clientId and Answer < s.Answer),clientID, questionID, AnswerFROM survey sORDER BY clientID,AnswerSELECT clientID, SUM(CASE WHEN RowNo=1 THEN Answer ELSE 0 END) AS Q_1_1,SUM(CASE WHEN RowNo=2 THEN Answer ELSE 0 END) AS Q_1_2,SUM(CASE WHEN RowNo=3 THEN Answer ELSE 0 END) AS Q_1_3,SUM(CASE WHEN RowNo=4 THEN Answer ELSE 0 END) AS Q_1_4FROM @tempGROUP BY clientIDoutput-----------------------clientID Q_1_1 Q_1_2 Q_1_3 Q_1_4----------- ----------- ----------- ----------- -----------1 1 2 4 52 1 5 8 03 2 4 6 94 8 0 0 05 1 5 9 0 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-02-05 : 04:55:01
|
quote: Originally posted by slimt_slimt
SELECT distinct(clientID) ,Q1_1 ,Q1_2 ,Q1_3 ,Q1_4from (select distinct(clientID) ,sum(case when QuestionID = 1 and Answer in (1,2,3,4,5,6,7,8,9,10) then Answer else 0 end) as Q1_1 ,sum(case when QuestionID = 1 and Answer in (1,2,3,4,5,6,7,8,9,10) then Answer else 0 end) as Q1_2 ,sum(case when QuestionID = 1 and Answer in (1,2,3,4,5,6,7,8,9,10) then Answer else 0 end) as Q1_3 ,sum(case when QuestionID = 1 and Answer in (1,2,3,4,5,6,7,8,9,10) then Answer else 0 end) as Q1_4 from dbo.survey group by clientid) as xgroup by x.clientID ,x.Q1_1 ,x.Q1_2 ,x.Q1_3 ,x.Q1_4order by clientID asc what am i doing wrong?
There is nothing wrong with the query besides the fact you are summing question 1 for all columns... ,sum(case when QuestionID = 1 and Answer in (1,2,3,4,5,6,7,8,9,10) then Answer else 0 end) as Q1_1 ,sum(case when QuestionID = 1 2 and Answer in (1,2,3,4,5,6,7,8,9,10) then Answer else 0 end) as Q1_2 ,sum(case when QuestionID = 1 3 and Answer in (1,2,3,4,5,6,7,8,9,10) then Answer else 0 end) as Q1_3 ,sum(case when QuestionID = 1 4 and Answer in (1,2,3,4,5,6,7,8,9,10) then Answer else 0 end) as Q1_4 E 12°55'05.25"N 56°04'39.16" |
 |
|
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2008-02-05 : 05:14:46
|
| visakh16: lovely. thank you very much |
 |
|
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2008-02-05 : 05:33:39
|
Problem begins if i add questionID = 2insert into survey (clientID, questionID, Answer) values (1, 2, 6)insert into survey (clientID, questionID, Answer) values (1, 2, 7)insert into survey (clientID, questionID, Answer) values (1, 2, 8)insert into survey (clientID, questionID, Answer) values (2, 2, 3)insert into survey (clientID, questionID, Answer) values (2, 2, 4)insert into survey (clientID, questionID, Answer) values (2, 2, 5)insert into survey (clientID, questionID, Answer) values (2, 2, 8)insert into survey (clientID, questionID, Answer) values (3, 2, 3)insert into survey (clientID, questionID, Answer) values (3, 2, 4)insert into survey (clientID, questionID, Answer) values (4, 2, 3)insert into survey (clientID, questionID, Answer) values (4, 2, 4)insert into survey (clientID, questionID, Answer) values (4, 2, 6)insert into survey (clientID, questionID, Answer) values (5, 2, 4) and the RowNo collapses. :( |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-05 : 05:37:28
|
| change insert temp step as follows:-INSERT INTO @temp (RowNo,clientID, questionID, Answer)SELECT (SELECT COUNT(*) + 1 FROM survey WHERE clientId=s.clientId and questionID=s.questionIDand Answer < s.Answer),clientID, questionID, AnswerFROM survey sORDER BY clientID,questionID,Answer |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-05 : 09:44:53
|
| full soln:-DECLARE @temp table(RowNo int,clientID int,questionID int,Answer int)INSERT INTO @temp (RowNo,clientID, questionID, Answer)SELECT (SELECT COUNT(*) + 1 FROM survey WHERE clientId=s.clientId and questionID=s.questionIDand Answer < s.Answer),clientID, questionID, AnswerFROM survey sORDER BY clientID,AnswerSELECT clientID, questionID, SUM(CASE WHEN RowNo=1 THEN Answer ELSE 0 END) AS Q_1_1,SUM(CASE WHEN RowNo=2 THEN Answer ELSE 0 END) AS Q_1_2,SUM(CASE WHEN RowNo=3 THEN Answer ELSE 0 END) AS Q_1_3,SUM(CASE WHEN RowNo=4 THEN Answer ELSE 0 END) AS Q_1_4FROM @tempGROUP BY clientID, questionID |
 |
|
|
|
|
|