有几种方法可以获得结果,所有这些方法都涉及row_number
为数据的每一行生成序列。
您可以将聚合函数与 CASE 表达式一起使用:
select
max(case when seq = 1 then columna end) columna_1,
max(case when seq = 1 then columnb end) columnb_1,
max(case when seq = 1 then columnc end) columnc_1,
max(case when seq = 2 then columna end) columna_2,
max(case when seq = 2 then columnb end) columnb_2,
max(case when seq = 2 then columnc end) columnc_2,
max(case when seq = 3 then columna end) columna_3,
max(case when seq = 3 then columnb end) columnb_3,
max(case when seq = 3 then columnc end) columnc_3
from
(
select columna, columnb, columnc,
row_number() over(order by columna) seq
from yourtable
) d;
请参阅SQL Fiddle with Demo。
您可以使用 PIVOT 函数,但您需要先对 3 列数据进行反透视,然后再应用 PIVOT。unpivot 过程将您的 3 列数据转换为多行。您可以使用 UNPIVOT 函数或 CROSS APPLY 来执行此操作:
select ColumnA_1, ColumnB_1, ColumnC_1,
ColumnA_2, ColumnB_2, ColumnC_2,
ColumnA_3, ColumnB_3, ColumnC_3
from
(
select col = col+'_'+cast(seq as varchar(10)),
value
from
(
select columna, columnb, columnc,
row_number() over(order by columna) seq
from yourtable
) d
cross apply
(
select 'ColumnA', columna union all
select 'ColumnB', columnb union all
select 'ColumnC', columnc
) c (col, value)
) s
pivot
(
max(value)
for col in (ColumnA_1, ColumnB_1, ColumnC_1,
ColumnA_2, ColumnB_2, ColumnC_2,
ColumnA_3, ColumnB_3, ColumnC_3)
) piv;
请参阅SQL Fiddle with Demo。如果您的数量有限或已知值,上述两个版本效果很好,但如果您有一个未知数,那么您将不得不考虑使用动态 SQL 来获得最终结果:
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT ',' + QUOTENAME(col+'_'+cast(seq as varchar(10)))
from
(
select row_number() over(order by columna) seq
from yourtable
) d
cross apply
(
select 'ColumnA', 1 union all
select 'ColumnB', 2 union all
select 'ColumnC', 3
) c (col, so)
group by col, seq, so
order by seq, so
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT ' + @cols + '
from
(
select col = col+''_''+cast(seq as varchar(10)),
value
from
(
select columna, columnb, columnc,
row_number() over(order by columna) seq
from yourtable
) d
cross apply
(
select ''ColumnA'', columna union all
select ''ColumnB'', columnb union all
select ''ColumnC'', columnc
) c (col, value)
) x
pivot
(
max(value)
for col in (' + @cols + ')
) p '
execute sp_executesql @query;
请参阅SQL Fiddle with Demo。所有版本都会给出一个结果:
| COLUMNA_1 | COLUMNB_1 | COLUMNC_1 | COLUMNA_2 | COLUMNB_2 | COLUMNC_2 | COLUMNA_3 | COLUMNB_3 | COLUMNC_3 |
| P | Q | R | S | T | U | V | W | X |