简体   繁体   中英

Converting 200+ rows to column in SQL Server using PIVOT

I have done some research and so far, this one makes sense to me:

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

However, I am wondering if there is any way to do this without having to declare the needed columns one by one, as I have more than 200 columns to retrieve.

Here's a sneak peek of what I have as a table:

ID   | Col_Name   | Col_Value    
------------------------------
1    | Item_Num   | 12345
2    | Item_Date  | 34567
3    | Item_Name  | 97454
4    | Item_App1  | 234567
5    | Item_App2  | 345678

Take note that I have 200+ distinct values for Col_Name and I want it to be Column fields.

I am using this one as of now but only for 5 columns:

SELECT * FROM 
(
   SELECT [ID], [Col_Name], [Col_Value] 
   FROM myTable
) AS a 
PIVOT 
(
   MAX([Col_Value])
   FOR [Col_Name] in ([Item_Num], [Item_Date], [Item_Name], [Item_App1], [Item_App2])
) AS p 
ORDER BY [ID]

Is there any way this could be done considering the performance? Thanks in advance.

You can use dynamic sql to create the list of col_name:

declare @pivot_col varchar(max);
declare @sql       varchar(max);
select  @pivot_col = string_agg( cast(col_name as varchar(max)), ', ') within group ( order by col_name ) from ( select distinct col_name from tmp_table ) A;

set @sql = 'SELECT * 
            FROM   (
                      SELECT [ID], [Col_Name], [Col_Value] 
                      FROM tmp_table
                   ) AS a 
                   PIVOT 
                   (
                      MAX([Col_Value])
                      FOR [Col_Name] in (' +  @pivot_col + ' )
                   ) AS p 
                   ORDER BY [ID]';
exec ( @sql );

The PIVOT / UNPIVOT operators are built on the principles of an Entity Attribute Value model (EAV). The idea behind an EAV model is that you can extend database entities without performing database schema changes. For that reason an EAV model stores all attributes of an entity in one table as key/value pairs.

If that is the idea behind your design then use the dynamic sql query i posted above, otherwise use a regular record with 200+ columns for each id, as suggested by Gordon.

You can read about the performance of PIVOT / UNPIVOT operatorshere .

EDIT: For sql server 2016 version:

declare @pivot_col varchar(max);
declare @sql       varchar(max);
select  @pivot_col = STUFF( (SELECT ',' + CAST(col_name AS VARCHAR(max)) AS [text()] FROM ( select distinct col_name from tmp_table ) A ORDER BY col_name FOR XML PATH('')), 1, 1, NULL);

set @sql = 'SELECT * 
            FROM   ( SELECT [ID], [Col_Name], [Col_Value] 
                     FROM tmp_table
                   ) AS a 
                   PIVOT 
                   (
                      MAX([Col_Value])
                      FOR [Col_Name] in (' +  @pivot_col + ' )
                   ) AS p 
                   ORDER BY [ID]';
exec ( @sql );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM