简体   繁体   English

如何使用一个表中的值作为 SQL 服务器中新表中的列名?

[英]How to use values from one table as column names in a new Table in SQL SERVER?

I have a table that looks like this:我有一个看起来像这样的表:

EmployeeName员工姓名 City城市
Maria玛丽亚 Chicago芝加哥
John约翰 Chicago芝加哥
Anna安娜 LA洛杉矶
Peter彼得 LA洛杉矶
Carrie嘉莉 NYC纽约市

And I need to create a table that will look like this:我需要创建一个如下所示的表:

Chicago芝加哥 LA洛杉矶 NYC纽约市
Maria玛丽亚 Anna安娜 Carrie嘉莉
John约翰 Peter彼得

I found this我找到了这个

SELECT (ColumnNames)  
FROM (TableName)  
PIVOT
(  
 AggregateFunction(ColumnToBeAggregated)
  FOR PivotColumn IN (PivotColumnValues)
) AS (Alias);   

But I am confused which aggregate function to use since the values are not numeric and also my table has too many Cities to write them manually.但是我很困惑要使用哪个聚合 function,因为这些值不是数字,而且我的表中有太多城市无法手动写入。

You can PIVOT with the addition of a new column.您可以添加一个新列 PIVOT。 In this simple case I used the window function dense_rank() over()在这个简单的例子中,我使用了 window function dense_rank() over()

Results结果

Declare @YourTable Table ([EmployeeName] varchar(50),[City] varchar(50))  Insert Into @YourTable Values 
 ('Maria','Chicago')
,('John','Chicago')
,('Anna','LA')
,('Peter','LA')
,('Carrie','NYC')
 

Select  *
 From  (
        Select EmployeeName
              ,City
              ,RN = dense_rank() over (partition by city order by EmployeeName desc) 
         From @YourTable
       ) src
 Pivot ( max(EmployeeName) for City in ( [Chicago],[LA],[NYC]) )pvt

Results结果

RN  Chicago LA      NYC
1   Maria   Peter   Carrie
2   John    Anna    NULL

UPDATE- Dynamic Approach更新-动态方法

Declare @SQL varchar(max) = '
Select  *
 From  (
        Select EmployeeName
              ,City
              ,RN = dense_rank() over (partition by city order by EmployeeName desc) 
         From YourTable
       ) src
 Pivot ( max(EmployeeName) for City in ( '+ (Select string_agg(quotename(City),',') From  (Select distinct City from YourTable) A) +' ) )pvt
'
Exec(@SQL)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据另一个表中与第一个表中的列名相关的值,更新一个表中的SQL Server表值 - Update SQL Server table values in one table based upon values in another table that relate to the column names in the first 如何基于SQL Server中当前表列的值使用与其他表不同的列值 - How to use different column values from other table based on value on current table column in SQL server 如何 select 列名来自 SQL 中另一个表的值? - How to select values where column names are from another table in SQL? 如何将新值从 SQL 服务器中的不同表添加到表中? - How to add New values into a table from a different table in SQL Server? SQL Server将表的列值转换为名称,如果没有列的值,则将0分配给新列 - SQL Server convert a table column values to names and assign 0 to the new columns if no value for a column 如何从SQL Server中传递的参数中选择表和列名? - How to select Table and Column Names from passed parameters in SQL Server? 如何从 SQL Server 中的表中获取列名? - How can I get column names from a table in SQL Server? 如何将列从一个表复制到 SQL 服务器中的另一个表 - How to copy column from one table into another table in SQL Server 将同一表中一列的值更新到 SQL Server 中的另一列 - Update values from one column in same table to another in SQL Server 如何使用另一个表的行值作为列名(动态)在SQL中创建新表? - How do I create a new table in SQL using the row values of another table as column names (dynamic)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM