2010-05-28 135 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