为了获得您想要的最终结果,您需要取消透视当前列,然后应用透视函数。但在您取消/透视您的数据之前,我建议您使用row_number()
为每一行生成一个唯一值。
有几种不同的方法可以获得结果,包括使用带有 CASE 表达式的聚合函数、静态 PIVOT 和动态 PIVOT。
使用 CASE 聚合:您可以先使用 UNION ALL 查询将多列转换为行,然后使用带有 CASE 表达式的聚合函数来获得结果:
;with cte as
(
select id, atr1, atr2, atr3, atr4,
row_number() over(order by id) seq
from yourtable
)
select s_no,
max(case when seq = 1 then value end) [1],
max(case when seq = 2 then value end) [2],
max(case when seq = 3 then value end) [3],
max(case when seq = 4 then value end) [4]
from
(
select seq, s_no = 'id', value = cast(id as varchar(5)), so = 1
from cte
union all
select seq, s_no = 'atr1', value = atr1, so = 2
from cte
union all
select seq, s_no = 'atr2', value = atr2, so = 3
from cte
union all
select seq, s_no = 'atr3', value = atr3, so = 4
from cte
union all
select seq, s_no = 'atr4', value = atr4, so = 5
from cte
) d
group by s_no, so
order by so;
请参阅带有演示的 SQL Fiddle
静态 UNPIVOT/PIVOT:如果要转换的值数量有限,则可以对查询进行硬编码。unpivot 的过程将转换您的多列id
, atr1
, atr2
, atr3
,atr4
并将它们转换为多行。您没有指定您使用的 SQL Server 版本,但这可以使用UNPIVOT
函数或使用CROSS APPLY
.
select seq, s_no, value, so
from
(
select id, atr1, atr2, atr3, atr4,
row_number() over(order by id) seq
from yourtable
) s
cross apply
(
select 'id', cast(id as varchar(5)), 1 union all
select 'atr1', atr1, 2 union all
select 'atr2', atr2, 3 union all
select 'atr3', atr3, 4 union all
select 'atr4', atr4, 5
) c (s_no, value, so);
请参阅SQL Fiddle with Demo。我使用 CROSS APPLY 和 UNION ALL 来选择每一列并将它们转换为多行。此查询会将您的数据转换为以下格式:
| SEQ | S_NO | VALUE | SO |
| 1 | id | 1 | 1 |
| 1 | atr1 | a | 2 |
| 1 | atr2 | bsdf | 3 |
| 1 | atr3 | csdfs | 4 |
| 1 | atr4 | djk | 5 |
| 2 | id | 5 | 1 |
将数据放入多行后,您可以应用 PIVOT 函数:
select s_no, [1], [2], [3], [4]
from
(
select seq, s_no, value, so
from
(
select id, atr1, atr2, atr3, atr4,
row_number() over(order by id) seq
from yourtable
) s
cross apply
(
select 'id', cast(id as varchar(5)), 1 union all
select 'atr1', atr1, 2 union all
select 'atr2', atr2, 3 union all
select 'atr3', atr3, 4 union all
select 'atr4', atr4, 5
) c (s_no, value, so)
) d
pivot
(
max(value)
for seq in ([1], [2], [3], [4])
) piv
order by so;
请参阅SQL Fiddle with Demo。
动态 UNPIVOT/PIVOT: 如果您要创建已知或有限数量的新列,但如果您有未知数量的值要转换为列,则上述方法非常有用,那么您将需要考虑使用动态 SQL。这将生成一个 sql 字符串,然后执行该字符串以获得最终结果:
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT ',' + QUOTENAME(seq)
from
(
select row_number() over(order by id) seq
from yourtable
)d
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT s_no,' + @cols + '
from
(
select seq, s_no, value, so
from
(
select id, atr1, atr2, atr3, atr4,
row_number() over(order by id) seq
from yourtable
) s
cross apply
(
select ''id'', cast(id as varchar(5)), 1 union all
select ''atr1'', atr1, 2 union all
select ''atr2'', atr2, 3 union all
select ''atr3'', atr3, 4 union all
select ''atr4'', atr4, 5
) c (s_no, value, so)
) x
pivot
(
max(value)
for seq in (' + @cols + ')
) p '
execute sp_executesql @query;
请参阅SQL Fiddle with Demo。所有版本都会给出一个结果:
| S_NO | 1 | 2 | 3 | 4 |
| id | 1 | 5 | 8 | 9 |
| atr1 | a | esdds | i | ads |
| atr2 | bsdf | f | j | sdf |
| atr3 | csdfs | gds | ksd | dfse |
| atr4 | djk | hkkj | lk | wer |