Click here to Skip to main content
16,000,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need help in writing a query.I have 4 tables which are related Works,WorkTypeProperties,WorkTypes,WorkProperties.

Now if i join all these tables i get the result using this query

select wp.WorkID,wp.WorkPropertyValue,IsValidated 
from   Works w
inner join (select WorkID,WorkPropertyValue,WorkTypePropertyID 
            from   WorkProperties) wp 
            on     w.WorkID=wp.WorkID
inner join (select WorkTypePropertyID,WorkTypeID 
            from   WorkTypeProperties) wtp 
            on     wtp.WorkTypePropertyID=wp.WorkTypePropertyID
inner join (select WorkTypeID 
            from   WorkTypes 
            where  DictionaryKey ='work_song') wt 
            on     wt.WorkTypeID=wtp.WorkTypeID


Result is

workid    workprop      Isvalidated
2         la la song    0  
2         234           0
2         2/8/2010      0
4         title         1
4         pop           1
4         3            1
4         2/22/2010     1


Now i want query for getting result like below

workid    workprop1    workprop2   workprop3   workprop4   Isvalidated 
2         la la song   null        234         2/8/2008    0
4         title        pop         3           2/22/2010   1


since i have max rows 4 in workid(2,4) i need to get 4 columns
Posted
Updated 17-Mar-10 22:05pm
v5

1 solution

What you need to do is to write a Pivot query. If you search Google for http://www.google.co.uk/search?hl=en&q=sql+server+pivot+query&meta=[^], you will get many examples of how to do this.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900