PIVOT没有按预期执行

时间:2010-05-28 10:38:05

标签: sql tsql 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

1 个答案:

答案 0 :(得分:2)

结果是:

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

:)

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

中号