将多列转换为单行

时间:2013-09-25 16:47:35

标签: sql sql-server pivot

我有需要将当前数据更改为所需数据的情况。

以下是数据

    ColumnA ColumnB ColumnC   
      P       Q       R   
      S       T       U   
      V       W       X

必需的格式是

 ColumnA1 ColumnB1 ColumnC1 ColumnA2 ColumnB2 ColumnC2   ColumnA3   ColumnB3 ColumnC3    
   P         Q         R       S        T         U            V        W       x

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

有几种方法可以获得结果,所有这些方法都涉及使用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 |
相关问题