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 |
|
ricc
Starting Member
16 Posts |
Posted - 2007-05-06 : 05:21:31
|
| Hi, I have the following two tablesPropertiesPropertyID__Name1____________nu12____________nu23____________nu34____________nu45____________nu5ImagesImageID__PropertyID__ImageUrl1___________1_________11.jpg2___________1_________21.jpg3___________1_________31.jpg5___________2_________52.jpg6___________3_________63.jpg7___________3_________73.jpg8___________3_________83.jpgI have to create from the above two table a query that has the following structure, the results have to have 4 Image Url columns even if the property only has a max of 3 imagesPropertyID__Name__Image1Url__Image2Url__Image3Url__Image4Url1____________nu1____11.jpg_____21.jpg_____31.jpg_____nullcan anyone provide any pointers?Many thanks |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-05-06 : 05:44:48
|
[code]select p.PropertyID, p.Name, Image1Url = max(case when row = 1 then i.ImageUrl end), Image2Url = max(case when row = 2 then i.ImageUrl end), Image3Url = max(case when row = 3 then i.ImageUrl end), Image4Url = max(case when row = 4 then i.ImageUrl end)from Properties p inner join ( select PropertyID, ImageUrl, row = (select count(*) from Images x where x.PropertyID = m.PropertyID and x.ImageID <= m.ImageID) from Images m ) i on p.PropertyID = i.PropertyIDgroup by p.PropertyID, p.Name[/code] KH |
 |
|
|
ricc
Starting Member
16 Posts |
Posted - 2007-05-06 : 06:27:09
|
| Thank you very much for your quick reply. Worked great!!! |
 |
|
|
|
|
|