Dilip
Dilip

Reputation: 747

Convert row value in to column in SQL server (PIVOT)

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

Answers (3)

Sudharsana Rajasekaran
Sudharsana Rajasekaran

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

Himanshu
Himanshu

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 ║
╚═══════╩═════════╩═══════╝

See this SQLFiddle

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);

See this SQLFiddle

Upvotes: 19

Sahil Sondhi
Sahil Sondhi

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

Related Questions