How do I get this:
entityid name stringvalue
----------- -------------------- --------------------
1 ShortDescription Coal
1 LongDescription BlackCoal
1 ShortDescription Gold
1 LongDescription WhiteGold
1 ShortDescription Steel
1 LongDescription StainlessSteel
To become this:
entityid ShortDescription LongDescription
----------- -------------------- --------------------
1 Coal BlackCoal
1 Gold WhiteGold
1 Steel StainlessSteel
The code I have so far is:
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
Many thanks everyone,
Matt.
The
pivotkeyword is for aggregation use pretty much exclusively. You’ll likely have to do this manually via subqueries.That aside, your table layout is a really bad idea. I’ve done this before, I know lots of other people that have done this before, it does NOT scale well. The biggest problem is that you can’t properly define indices on your data. You should seriously consider just changing the table directly to your “pivoted” format as that’s a proper relational style for it anyways.