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 |
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2011-01-07 : 01:13:48
|
| Hi everyone! Hope you are doing well and wish you have a good day! :)Please go through the following data and let me know how can i get the result set as mentioned below:create table universities (UniversityID int, UniversityName varchaR(100))Create table URegions (ID int, UniversityID int, RegionID int)Insert into universities values (1,'abc')Insert into universities values (2,'xyz')Insert into URegions values ( 1,1,1)Insert into URegions values ( 2,1,2)Insert into URegions values ( 3,1,4)Insert into URegions values ( 5,2,1)Insert into URegions values ( 6,2,3)Select U.UniversityName ,(Case when UR.RegionID=1 then 'Yes' else 'No' End) as Region1 ,(Case when UR.RegionID=2 then 'Yes' else 'No' End) as Region2 ,(Case when UR.RegionID=3 then 'Yes' else 'No' End) as Region3 ,(Case when UR.RegionID=4 then 'Yes' else 'No' End) as Region4from universities U inner Join URegions UR on U.UniversityID=UR.UniversityIDThe result set of the above query is: UniversityName Region1 Region2 Region3 Region4abc Yes No No Noabc No Yes No Noabc No No No Yesxyz Yes No No Noxyz No No Yes NoHowever i want the result set should be like UniversityName Region1 Region2 Region3 Region4abc Yes Yes No Yesxyz Yes No Yes Nomany thanks! |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-01-07 : 01:56:25
|
[code]SelectU.UniversityName,max(Case when UR.RegionID=1 then 'Yes' else 'No' End) as Region1,max(Case when UR.RegionID=2 then 'Yes' else 'No' End) as Region2,max(Case when UR.RegionID=3 then 'Yes' else 'No' End) as Region3,max(Case when UR.RegionID=4 then 'Yes' else 'No' End) as Region4from universities Uinner Join URegions UR on U.UniversityID=UR.UniversityIDgroup by U.UniversityName[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2011-01-07 : 02:06:42
|
| @KH Many thanks! Cheers! |
 |
|
|
|
|
|
|
|