SQL 服务器使用 ID 作为列名将行转置为列

SQL Server Transpose Rows into Columns using IDs as Column Names

我有一个包含以下字段的大文件:

Table 1:

+---------+--------+-----------+
| User_Id | Key_Id | Value     | 
+---------+--------+-----------+
| 100     | 74     | 37        |
| 100     | 65     | Male      |
| 100     | 279    | G235467   |
+---------+--------+-----------+

我还有另一个文件告诉每个 'Key_Id' 叫什么(它们是列名)例如

Table 2:

+--------+------------------+
| Key_Id | Key              |
+--------+------------------+
| 65     | Gender           |
| 66     | Height           |
| 74     | Age              |
| 279    | ReferenceNo      |

我想使用 table 2 的键列中找到的 Key_Id 名称创建一个 table,将 table 1 中的所有值转置为 table 2,但也包括 table 1 中的 User_Id,因为这与个人有关。

PS。 Table 2 有将近 300 个键需要转换成单独的字段

所以最终我想要一个看起来像这样的 table:

+---------+---------+--------+-------+--------------+--------+
| User_Id | Gender  | Height | Age   | ReferenceNo  |  etc   |
+---------+---------+--------+-------+--------------+--------+
| 100     | Male    |        | 37    | G235467      |        |

因此每个 User_Id 都是一行,所有键都是具有各自值的列

您可以如下使用数据透视表:

Select * from (
    Select u.UserId, k.[key], u.[Value] from table1 u
       join table2 k on u.keyid = k.keyid   ) a
pivot ( max([Value]) for [key] in ([Gender], [Height], [Age], [ReferenceNo]) ) p

对于动态键列表,您可以使用动态 sql,如下所示:

Declare @cols1 varchar(max)
Declare @query nvarchar(max)

Select @cols1 = stuff((select ','+QuoteName([Key]) from table2 group by [Key] for xml path('')),1,1,'')

Set @Query = 'Select * from (
    Select u.UserId, k.[key], u.[Value] from table1 u
       join table2 k on u.keyid = k.keyid   ) a 
pivot ( max([Value]) for [key] in (' + @cols1 + ') ) p '

Select @Query  --Check the generated query and execute by uncommenting below query
--exec sp_executesql @Query 

您需要获取要在 T-SQL 中的 PIVOT/UNPIVOT 运算符中使用的那 300 个键名的逗号分隔列表,如此处所述

https://docs.microsoft.com/en-us/sql/t-sql/queries/from-using-pivot-and-unpivot

您可以使用如下动态 sql 查询。

查询

declare @sql as varchar(max);

select @sql = 'select t1.[User_Id], ' + stuff((select +
    ', max(case t2.[Key_Id] when ' + cast([Key_Id] as varchar(100)) + 
    ' then t1.[Value] end) as [' + [Key] + '] '
    from Table2 
    for xml path('')
), 1, 2, '') + 
'from Table1 t1 left join Table2 t2 on t1.[Key_Id] = t2.[Key_Id] group by t1.[User_Id];'

exec(@sql);

Find a demo here