【发布时间】:2010-05-28 10:17:42
【问题描述】:
我如何得到这个:
entityid name stringvalue
----------- -------------------- --------------------
1 ShortDescription Coal
1 LongDescription BlackCoal
1 ShortDescription Gold
1 LongDescription WhiteGold
1 ShortDescription Steel
1 LongDescription StainlessSteel
变成这样:
entityid ShortDescription LongDescription
----------- -------------------- --------------------
1 Coal BlackCoal
1 Gold WhiteGold
1 Steel StainlessSteel
我目前的代码是:
select *
from (select entityid, cast(name as nvarchar(20)) as name, cast(stringvalue as nvarchar(20)) as stringvalue from metapropertyvalue) as d
pivot
(
max(stringvalue)
for [name] in ([ShortDescription],[LongDescription])
)
as p
非常感谢大家,
马特。
【问题讨论】:
-
尝试搜索“Pivot T-SQL”——这方面有很多问题。
-
是的,我有,但问题是我似乎没有可以在聚合函数中使用的字段。即:stackoverflow.com/questions/2928022/…
-
我上面的评论是对 David M 的评论,而不是 Mitch Wheat 的评论。
-
马特,这可能会有所帮助:simple-talk.com/sql/t-sql-programming/…
-
这被否决的任何原因?如果解决方案这么简单,链接只是链接到它...