SQL Server SQL convert column into

14ifxucb  于 2023-08-02  发布在  其他
关注(0)|答案(1)|浏览(84)

I have a table like below in SQL Server 2010

But I need an output like below

Please help me to solve, I need a select statement like how we are using pivot in Excel.

snvhrwxg

snvhrwxg1#

You can use Pivot table as :

SELECT [ID], [Data1] AS [Type1], [Data2] AS [Type2], [Data3] AS [Type3], [Data4] AS [Type4]
FROM (
    SELECT [ID], [Type], [Data]
    FROM table1
) AS SourceTable
PIVOT (
    MAX([Data])
    FOR [Type] IN ([Data1], [Data2], [Data3], [Data4])
) AS PivotTable;

I hope this will help you!!

相关问题