Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
pamyral_279
Posting Yak Master
143 Posts |
Posted - 2010-04-01 : 03:58:30
|
| Hi all,I have table as :ID--------IP---------QuestionID----Answer---OtherAnswer---IsSpecial1---203.155.168.11--------1----------A---------0---------------01---203.155.168.11--------1----------A1--------0---------------03---203.118.118.13--------1----------B---------0---------------04---203.137.168.19--------1----------Y--------"123456"---------15---166.122.168.35--------1----------F---------0---------------06---223.115.168.77--------2----------Y1-------"987654"---------1Expected output:* if isspecial=1 i will replace otheranswer into answer:ID--------IP---------QuestionID----Answer-----OtherAnswer---IsSpecial1---203.155.168.11--------1----------A---------0---------------01---203.155.168.11--------1----------A1--------0---------------03---203.118.118.13--------1----------B---------0---------------04---203.137.168.19--------1----------"123456"--"123456"---------15---166.122.168.35--------1----------F---------0---------------06---223.115.168.77--------2----------"987654"--"987654"---------1* I want to group by ip ,add to "Final answer"ID--------IP---------QuestionID----Answer----IsSpecial-- FinalAnswer1---203.155.168.11--------1----------A---------0--------------A+A13---203.118.118.13--------1----------B---------0---------------B4---203.137.168.19--------1---------"123456"---1-------------"123456"5---166.122.168.35--------1----------F---------0--------------F6---223.115.168.77--------2---------"987654"---1--------------"987654"Any one help me ??Thank you very much ! |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-04-01 : 05:20:22
|
Here is the solutionCREATE TABLE tbl (ID int, IP varchar(20), QuestionID int, Answer varchar(20), OtherAnswer varchar(100) ,IsSpecial bit)INSERT INTO tblSELECT 1,'203.155.168.11',1,'A','0',0UNION ALLSELECT 1,'203.155.168.11',1,'A1','0',0UNION ALLSELECT 3,'203.118.118.13',1,'B','0',0UNION ALLSELECT 4,'203.137.168.19',1,'Y','"123456"',1UNION ALLSELECT 5,'166.122.168.35',1,'F','0',0UNION ALLSELECT 6,'223.115.168.77',2,'Y1','"987654"',1with cte as(select id, ip, questionid, CASE WHEN IsSpecial = 1 THEN OtherAnswer ELSE Answer END AS Answer, OtherAnswer, IsSpecial,ROW_NUMBER() OVER(Partition by ip order by id) seqFROM tbl)select id, ip, questionid, Answer,STUFF((SELECT '+'+Answer FROM CTE WHERE id=c.id FOR XML PATH('')),1,1,'') as FinalAnswer, isspecialFROM cte cWHERE seq = 1 order by idWhat is logic for final answer to be A+B as if you want group by ipaccording to me output should be like below1 203.155.168.11 1 A A+A1 03 203.118.118.13 1 B B 04 203.137.168.19 1 "123456" "123456" 15 166.122.168.35 1 F F 06 223.115.168.77 2 "987654" "987654" 1Vaibhav T |
 |
|
|
pamyral_279
Posting Yak Master
143 Posts |
Posted - 2010-04-01 : 05:35:40
|
| Good sql statement ! Can i replace your tbl with "Select * from my table" ?? How can i do ?Because my data join many table ! i get a example !with cte as(select id, ip, questionid, CASE WHEN IsSpecial = 1 THEN OtherAnswer ELSE Answer END AS Answer, OtherAnswer, IsSpecial,ROW_NUMBER() OVER(Partition by ip order by id) seqFROM "Select * from ...inner join...." )select id, ip, questionid, Answer,STUFF((SELECT '+'+Answer FROM CTE WHERE id=c.id FOR XML PATH('')),1,1,'') as FinalAnswer, isspecialFROM cte cWHERE seq = 1 order by idIs that ok ? All Field in my select are same all field in your tbl !Thank for your attension ! |
 |
|
|
pamyral_279
Posting Yak Master
143 Posts |
Posted - 2010-04-01 : 05:47:27
|
| I did it !! I really want to say thank you very much !! ^^ |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-04-01 : 05:58:50
|
You are welcome I am glad that i could help you...Vaibhav T |
 |
|
|
pamyral_279
Posting Yak Master
143 Posts |
Posted - 2010-04-01 : 23:03:15
|
| Hi, i have discover that it occurs small problem,if same IP but different QuestionID ,your statement get only one record.Example :ID--------IP---------QuestionID----Answer---OtherAnswer---IsSpecial1---203.155.168.11--------1----------A---------0---------------01---203.155.168.11--------1----------A1--------0---------------03---203.118.118.13--------1----------B---------0---------------04---203.137.168.19--------1----------Y--------"123456"---------15---203.155.168.11--------2----------OP--------0---------------06---203.155.168.11--------2----------HD-------"987654"---------1My problem group by Question ID:Expected output:ID--------IP---------QuestionID--------IsSpecial-- FinalAnswer1---203.155.168.11--------1-----------------0---------------A+A15---203.155.168.11--------2-----------------1---------------OP+"987654"Can you correct help me ??THank you very much. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-04-01 : 23:09:29
|
try change toPartition by ip, QuestionID KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
pamyral_279
Posting Yak Master
143 Posts |
Posted - 2010-04-01 : 23:30:45
|
quote: Originally posted by khtan try change toPartition by ip, QuestionID KH[spoiler]Time is always against us[/spoiler]
I have just try your suggestion ! But still wrong with "Final Answer" field ! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-02 : 02:56:37
|
| [code]DECLARE @IPTable table(ID int,IP varchar(20),QuestionID int,Answer varchar(5),OtherAnswer varchar(6),IsSpecial bit)INSERT @IPTableSELECT 1,'203.155.168.11',1,'A','0',0 union allSELECT 1,'203.155.168.11',1,'A1','0',0 union allSELECT 3,'203.118.118.13',1,'B','0',0 union allSELECT 4,'203.137.168.19',1,'Y','123456',1 union allSELECT 5,'203.155.168.11',2,'OP','0',0 union allSELECT 6,'203.155.168.11',2,'HD','987654',1 ;With CTE (RowNo,ID,IP,QuestionID,Answer,IsSpecial) AS(SELECT ROW_NUMBER() OVER (PARTITION BY IP,QuestionID ORDER BY Answer),ID,IP,QuestionID,CASE WHEN IsSpecial=1 THEN OtherAnswer ELSE Answer END,IsSpecial-- FinalAnswerFROM @IPTable)SELECT ID,IP,QuestionID,Answer,IsSpecial,STUFF((SELECT '+' + Answer FROM CTE WHERE IP=c.IP AND QuestionID=c.QuestionID FOR XML PATH('')),1,1,'') AS FinalAnswerFROM CTE cWHERE c.RowNo=1ORDER BY IDoutput---------------------------------ID IP QuestionID Answer IsSpecial FinalAnswer1 203.155.168.11 1 A 0 A+A13 203.118.118.13 1 B 0 B4 203.137.168.19 1 123456 1 1234566 203.155.168.11 2 987654 1 OP+987654[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
pamyral_279
Posting Yak Master
143 Posts |
Posted - 2010-04-02 : 04:01:05
|
| Thanks visakh16 ^^ ! Problem has been solved ! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-02 : 04:04:40
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|