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 |
cjcclee
Starting Member
33 Posts |
Posted - 2013-11-18 : 11:26:37
|
Hi, all I need help on this query. ID1 yes ID1 No ID2 No ID3 Yes I want the result be: ID1 Yes ID2 No ID3 Yes each ID may have multiple yes/no, at least 1 yes, the result will be yes. I do not want cascade string, any better way to do it?Thanks! |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2013-11-18 : 11:37:51
|
declare @sample table (id varchar(30), col2 varchar(30))insert @sampleselect 'ID1', 'Yes' union allselect 'ID1', 'No' union allselect 'ID2', 'No' union allselect 'ID3', 'Yes'selectid,max(col2) as col2from @samplegroup by id Too old to Rock'n'Roll too young to die. |
|
|
cjcclee
Starting Member
33 Posts |
Posted - 2013-11-18 : 12:50:36
|
Thanks for help. I may not say clearly, It is not always be Max(col).each ID have multiple response for Yes/No, as long as one yes found, that ID will be yes. for example:ID1 YesID1 NoId2 NoId2 yesId3 NoId4 YesThe result need beId1 yesid2 yesId3 noId4 yesquote: Originally posted by webfred declare @sample table (id varchar(30), col2 varchar(30))insert @sampleselect 'ID1', 'Yes' union allselect 'ID1', 'No' union allselect 'ID2', 'No' union allselect 'ID3', 'Yes'selectid,max(col2) as col2from @samplegroup by id Too old to Rock'n'Roll too young to die.
|
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2013-11-18 : 18:36:50
|
What happens when you run Webfred's query on your data? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-19 : 01:47:49
|
quote: Originally posted by cjcclee Thanks for help. I may not say clearly, It is not always be Max(col).each ID have multiple response for Yes/No, as long as one yes found, that ID will be yes. for example:ID1 YesID1 NoId2 NoId2 yesId3 NoId4 YesThe result need beId1 yesid2 yesId3 noId4 yesquote: Originally posted by webfred declare @sample table (id varchar(30), col2 varchar(30))insert @sampleselect 'ID1', 'Yes' union allselect 'ID1', 'No' union allselect 'ID2', 'No' union allselect 'ID3', 'Yes'selectid,max(col2) as col2from @samplegroup by id Too old to Rock'n'Roll too young to die.
the given suggestion would still work rightSee illustration belowdeclare @sample table (id varchar(30), col2 varchar(30))insert @sampleselect 'ID1', 'Yes' union allselect 'ID1', 'No' union allselect 'ID2', 'No' union allselect 'ID2', 'Yes' union allselect 'ID3', 'No' union allselect 'ID4', 'Yes'selectid,max(col2) as col2from @samplegroup by idoutput---------------id col2---------------ID1 YesID2 YesID3 NoID4 Yes ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|