Reputation: 747
I have table in SQL Server called test having 3 column
| ITEM | ATTRIBUTE | VALUE |
-----------------------------
| item1 | Quality | A |
| item1 | color | Red |
| item2 | Quality | B |
| item2 | color | Black |
I want output like this:
| ITEM | QUALITY | COLOR |
---------------------------
| item1 | A | Red |
| item2 | B | Black |
How can I get this in SQL Server.
Upvotes: 12
Views: 54824
Reputation: 328
String Aggregation can be used as well,
SELECT item, STRING_AGG(CAST(q AS STRING)) AS Quantity, STRING_AGG(CAST(c AS STRING)) AS Color
FROM
(
SELECT
item,
CASE WHEN attribute = 'Quality' THEN value ELSE NULL END AS q,
CASE WHEN attribute = 'Color' THEN value ELSE NULL END AS c
FROM MyTable
) temp
GROUP BY item
Upvotes: 0
Reputation: 32602
Try this one:
SELECT *
FROM (SELECT Item, attribute, value FROM MyTable) AS t
PIVOT
(
MAX(value)
FOR attribute IN([Quality], [Color])
) AS p;
Output:
╔═══════╦═════════╦═══════╗
║ ITEM ║ QUALITY ║ COLOR ║
╠═══════╬═════════╬═══════╣
║ item1 ║ A ║ Red ║
║ item2 ║ B ║ Black ║
╚═══════╩═════════╩═══════╝
You can also use this dynamic query if you don't know the specific value of attribute
:
DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(attribute)
from MyTable
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT Item,' + @cols + '
from
(
Select Item, attribute , value
from MyTable
) dta
pivot
(
MAX(Value)
for attribute in (' + @cols + ')
) pvt '
execute(@query);
Upvotes: 19
Reputation: 41
This is a bit of a hacky solution for mysql as PIVOT doesn't work in it.
select item, GROUP_CONCAT('',q) as Quantity, GROUP_CONCAT('',c) as Color from
(select item ,CASE WHEN attribute = 'Quality' THEN value ELSE NULL END as q, CASE WHEN attribute = 'Color' THEN value ELSE NULL END as c
from MyTable
) temp
group by item
Issue with this solution is that you should know all the distinct values of attribute column.
You can try it here.
Upvotes: 4