需要查询 [英] Need query

查看:70
本文介绍了需要查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编写查询时需要帮助.我有4个与Works,WorkTypeProperties,WorkTypes,WorkProperties相关的表.

现在,如果我联接所有这些表,则可以使用此查询获取结果

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



由于我在workid(2,4)中有最多4行,因此我需要获得4列.



since i have max rows 4 in workid(2,4) i need to get 4 columns

推荐答案

您需要做的是编写一个Pivot查询.如果您在Google中搜索 http://www.google. co.uk/search?hl=zh-CN&q=sql+server+pivot+query&meta= [
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.


这篇关于需要查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆