没有聚合 SQL 服务器枢轴 table

Without aggregation SQL Server pivot table

Table 姓名 int_table。列为 col_name, col_value

col_name    col_value
----------------------
 A            aa
 B            bb
 C            cc
 D            dd
 D            ddd
 E            eee

我需要这样的输出

A        B          C           D        E
---------------------------------------------
aa       bb         cc          dd       eee
                                ddd     

这是示例值。我的 table 有 30 多行这样的。

这是我的尝试:

select * 
from int_table 
pivot (max(col_value) 
       for col_name in (A, B, C, D, E)) as tt

使用聚合函数显示错误。

如何使用 pivot 获得此解决方案?

我正在使用 SQL Server 2012

试试这个:

select A,B,C,D,E        
from (
   select col_name, col_value,
          row_number() over (partition by col_name order by col_value) as rn
   from int_table
) as src
pivot (max(col_value) for col_name in (A,B,C,D,E)) as pvt

这里的技巧是使用由 ROW_NUMBER window 函数生成的计算列,以区分具有相同 col_name 值的记录。

Demo here