1

我正在尝试使用枢轴来获取差异格式的信息。

这是我的桌子:

    CREATE TABLE yourtable
([case] int, [category] varchar(4))
    ;

    INSERT INTO yourtable
([case], [category])
    VALUES
(1, 'xx'),
(1, 'xyx'),
(1, 'abc'),
(2, 'ghj'),
(2, 'asdf'),
(3, 'dfgh')
    ; 

这是我由 bluefeet 提供的枢轴命令:

    DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

    select @cols = STUFF((SELECT distinct ',' + QUOTENAME('cat'+cast(seq as    
    varchar(10))) 
                from
                (
                  select row_number() over(partition by [case] 
                                           order by category) seq
                  from yourtable
                ) d
        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,'')

    set @query = 'SELECT [case],' + @cols + ' 
        from 
        (
          SELECT [case], category,
            ''cat''+
              cast(row_number() over(partition by [case] 
                                     order by category) as varchar(10)) seq
          FROM yourTable
        ) x
        pivot 
        (
            max(category)
            for seq in (' + @cols + ')
        ) p '

    execute sp_executesql @query;

输出很好,是我需要的格式。

CASE    CAT1    CAT2    CAT3
1        abc    xx    xyx
2        asdf   ghj   (null)
3        dfgh   (null)   (null)

但是,我还需要向表中添加其他列。修改后的表如下,但我不确定如何将其添加到 QUOTENAME。

    CREATE TABLE yourtable
    ([case] int, [category] varchar(4), [status] varchar(4))
    ;

    INSERT INTO yourtable
    ([case], [category], [status])
    VALUES
    (1, 'xx', '00'),
    (1, 'xyx', '01'),
    (1, 'abc', '00'),
    (2, 'ghj', '01'),
    (2, 'asdf', '00'),
    (3, 'dfgh', '01')
     ; 

如何才能做到这一点?我应该添加一个额外的 QUOTENAME 命令吗?结果应该是:

    CASE    CAT1    status1    CAT2      status2      CAT3      status3
    1       abc     00         xx          00         xyx        01
    2       asdf    00         ghj         01       (null)    (null)
    3       dfgh    01        (null)     (null)     (null)    (null)
4

1 回答 1

4

由于您现在有两列要 PIVOT,因此您可以首先将categorystatus列取消旋转为具有多行的单列。

有几种不同的方法可以取消透视数据,可以使用 UNPIVOT 或 CROSS APPLY。基本语法将是:

select [case],
  col+cast(seq as varchar(10)) seq,
  value
from
(
  SELECT [case], status, category,
    row_number() over(partition by [case] 
                      order by status) seq
  FROM yourTable
) d
cross apply
(
  select 'cat', category union all
  select 'status', status
) c (col, value)

请参阅带有演示的 SQL Fiddle这会将您的多列数据转换为如下所示的内容:

| CASE |     SEQ | VALUE |
|------|---------|-------|
|    1 |    cat1 |    xx |
|    1 | status1 |    00 |
|    1 |    cat2 |   abc |
|    1 | status2 |    00 |
|    1 |    cat3 |   xyx |
|    1 | status3 |    01 |
|    2 |    cat1 |  asdf |
|    2 | status1 |    00 |

一旦数据采用这种格式,您就可以对其应用 PIVOT 函数。

SELECT [case], cat1, status1, cat2, status2, cat3, status3
FROM
(
  select [case],
    col+cast(seq as varchar(10)) seq,
    value
  from
  (
    SELECT [case], status, category,
      row_number() over(partition by [case] 
                        order by status) seq
    FROM yourTable
  ) d
  cross apply
  (
    select 'cat', category union all
    select 'status', status
  ) c (col, value)
) x
PIVOT
(
   max(value)
   for seq in (cat1, status1, cat2, status2, cat3, status3)
)p;

请参阅带有演示的 SQL Fiddle

然后可以将其转换为动态 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(partition by [case] 
                                               order by category) seq
                      from yourtable
                    ) d
                    cross apply
                    (
                      select 'cat', 1 union all
                      select 'status', 2 
                    ) c (col, so)
                    group by seq, col, so
                    order by seq, so
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT [case],' + @cols + ' 
            from 
            (
              select [case],
                col+cast(seq as varchar(10)) seq,
                value
              from
              (
                SELECT [case], status, category,
                  row_number() over(partition by [case] 
                                    order by status) seq
                FROM yourTable
              ) d
              cross apply
              (
                select ''cat'', category union all
                select ''status'', status
              ) c (col, value)
            ) x
            pivot 
            (
                max(value)
                for seq in (' + @cols + ')
            ) p '

execute sp_executesql @query;

请参阅SQL Fiddle with Demo 最终结果将是:

| CASE | CAT1 | STATUS1 |   CAT2 | STATUS2 |   CAT3 | STATUS3 |
|------|------|---------|--------|---------|--------|---------|
|    1 |   xx |      00 |    abc |      00 |    xyx |      01 |
|    2 | asdf |      00 |    ghj |      01 | (null) |  (null) |
|    3 | dfgh |      01 | (null) |  (null) | (null) |  (null) |
于 2013-10-31T19:52:04.133 回答