2010-05-28 75 views
2

对不起,以前不清楚的问题;希望我能重新开始......PIVOT未按预期执行

我有这样的数据:

entityid name     stringvalue 
----------- -------------------- -------------------- 
1   ShortDescription  Coal 
1   LongDescription  BlackCoal 
1   ShortDescription  Gold 
1   LongDescription  WhiteGold 
1   ShortDescription  Steel 
1   LongDescription  StainlessSteel 

而这个查询:

select * 
from 
(
    select entityid, name, stringvalue as stringvalue 
    from mytable 
) as d 
pivot 
(
    min([stringvalue]) 
    for [name] in ([ShortDescription],[LongDescription]) 
) 
as p 

生产这种输出:

entityid ShortDescription LongDescription 
-------- ---------------- --------------- 
1  Coal    BlackCoal 

有人能告诉我为什么其他行不在制作中?我期待看到:

entityid ShortDescription LongDescription 
-------- ---------------- --------------- 
1  Coal    BlackCoal 
1  Gold    WhiteGold 
1  Steel   StainlessSteel 

回答

2

答案竟然是这样的:

select * 
from 
(
    select entityid, [name], stringvalue as stringvalue 
    from mytable 
) as d 
pivot 
(
    min(stringvalue) 
    for [name] in ([ShortDescription],[LongDescription]) 
) 
as p 

:)

的缺陷是,输入表应该有1,1,2, 2,3,3分别为entityid行。

M