1

我有下表和以下示例数据

ID  Language       Question       SubQuestion     SubSubQuestion    TotalCount  TotalPercent
3      E               9               0                1             88527            73%
3      E               9               0                2             19684            16%
3      E               9               0                3             12960            11%
3      E               9               0                9              933              1%

我想要这样的一排

    ID  Language        TotalCount901   TotalPercent901     TotalCount902   TotalPercent902 TotalCount903   TotalPercent903
     3       E            88527           73%                 19684             16%              12960              11%

我已经厌倦了使用 pivot 命令,但它不适合我。

4

1 回答 1

2

我根据您的列名做了一些假设,但看起来您想使用与此类似的东西。这将同时应用 anUNPIVOT和 aPIVOT来获取您请求的列中的值:

select *
from
(
  select id,
    language,
    col + cast(QUESTION as varchar(10))
      +cast(subquestion as varchar(10))
      +cast(SubSubQuestion as varchar(10)) col,
    value
  from
  (
    select id, language,
      cast(TotalCount as varchar(10)) TotalCount, 
      totalPercent,
      question, subquestion, SubSubQuestion
    from yourtable
  ) usrc
  unpivot
  (
    value 
    for col in (totalcount, totalpercent)
  ) un
) srcpiv
pivot
(
  max(value)
  for col in ([TotalCount901], [totalPercent901], 
              [TotalCount902], [totalPercent902], 
              [TotalCount903], [totalPercent903],
              [TotalCount909], [totalPercent909])
) p

请参阅带有演示的 SQL Fiddle

注意:在执行时UNPIVOT,列需要是相同的数据类型。如果不是,那么您将需要转换/强制转换以使数据类型相同。

如果要转换的值数量未知,可以使用动态 sql:

DECLARE @query  AS NVARCHAR(MAX),
    @colsPivot as  NVARCHAR(MAX)


select @colsPivot 
  = STUFF((SELECT  ',' 
             + QUOTENAME(c.name +
                        cast(QUESTION as varchar(10))
                        +cast(subquestion as varchar(10))
                        +cast(SubSubQuestion as varchar(10)))
           from yourtable t
           cross apply sys.columns as C
           where C.object_id = object_id('yourtable') and
              C.name in ('TotalCount', 'TotalPercent')
           group by c.name, t.question, t.subquestion, t.subsubquestion
           order by t.SubSubQuestion
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')


set @query = 'select *
              from
              (
                select id,
                  language,
                  col + cast(QUESTION as varchar(10))
                    +cast(subquestion as varchar(10))
                    +cast(SubSubQuestion as varchar(10)) col,
                  value
                from
                (
                  select id, language,
                    cast(TotalCount as varchar(10)) TotalCount, 
                    totalPercent,
                    question, subquestion, SubSubQuestion
                  from yourtable
                ) usrc
                unpivot
                (
                  value 
                  for col in (totalcount, totalpercent)
                ) un
              ) srcpiv
            pivot 
            (
                max(value)
                for col in (' + @colsPivot + ')
            ) p '

execute(@query)

请参阅带有演示的 SQL Fiddle

于 2012-11-01T20:12:14.563 回答