9

我有一个表格,其中包含个人进行的活动的详细信息 - 该表格的内容类似于以下内容:

| 人 | 类别 | 活动 |
--------------------------------------
| 用户名1 | X | X1 |
| 用户名1 | 是 | Y1 |
| 用户名1 | Z | Z1 |

我需要一个可以产生类似以下内容的 SQL 查询,我们将不胜感激:

| 人 | 第一类 | Cat1_Act|Cat2 | Cat2_Act| 三类 | Cat3_Act |
-------------------------------------------------- -------------
| 用户名1 | X | X1 | 是 | Y1 | Z | Z1 |

我了解通过阅读许多帖子可以使用 PIVOT 来实现这一点,但我没有找到接近我需要的解决方案,因为大多数解决方案通常使用值,例如“X”、“Y”、“Z” (在我的示例表中)作为表头,但理想情况下我希望能够为包含新列的表头指定名称(希望这一切都有意义,有人可以提供帮助:-))

4

2 回答 2

11

有几种方法可以获得所需的结果。如果您想要 PIVOT 到列中的值数量有限,那么您可以通过几种不同的方式对查询进行硬编码。

使用 CASE 聚合函数

select 
  person,
  max(case when seq = 1 then category end) Cat1,
  max(case when seq = 1 then activity end) Cat1_Act,
  max(case when seq = 2 then category end) Cat2,
  max(case when seq = 2 then activity end) Cat2_Act,
  max(case when seq = 3 then category end) Cat3,
  max(case when seq = 3 then activity end) Cat3_Act
from
(
  select person, category, activity,
    row_number() over(partition by person
                      order by category) seq
  from yourtable
) d
group by person;

请参阅SQL Fiddle with Demo。通过为每个用户的每个类别分配一个序列或 row_number,您可以使用此行号将行转换为列。

静态枢轴:

如果要应用 PIVOT 函数,那么我首先建议将categoryandactivity列取消透视到多行,然后应用 pivot 函数。

;with cte as
(
  select person, category, activity,
    row_number() over(partition by person
                      order by category) seq
  from yourtable
)
select person,
  cat1, cat1_act, 
  cat2, cat2_act,
  cat3, cat3_act
from
(
  select t.person, 
    col = case 
     when c.col = 'cat' then col+cast(seq as varchar(10))
      else 'cat'+cast(seq as varchar(10))+'_'+col
    end,
    value
  from cte t
  cross apply
 (
    select 'cat', category union all
    select 'act', activity
  ) c (col, value)
) d
pivot
(
  max(value)
  for col in (cat1, cat1_act, cat2, cat2_act,
              cat3, cat3_act)
) piv;

请参阅带有演示的 SQL Fiddle

动态 PIVOT:最后,如果您有未知数量的值,那么您可以使用动态 SQL 来获取结果:

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

select @cols = STUFF((SELECT ',' 
                          + QUOTENAME(case 
                                       when d.col = 'cat' then col+cast(seq as varchar(10))
                                        else 'cat'+cast(seq as varchar(10))+'_'+col end) 
                    from 
                    (
                      select row_number() over(partition by person
                                                order by category) seq
                      from yourtable
                    ) t
                    cross apply
                    (
                      select 'cat', 1 union all
                      select 'act', 2
                    ) d (col, so)
                    group by col, so, seq
                    order by seq, so
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT person, ' + @cols + ' 
            from 
            (
              select t.person, 
                col = case 
                 when c.col = ''cat'' then col+cast(seq as varchar(10))
                  else ''cat''+cast(seq as varchar(10))+''_''+col
                end,
                value
              from 
              (
                select person, category, activity,
                  row_number() over(partition by person
                                    order by category) seq
                from yourtable
              ) t
              cross apply
             (
                select ''cat'', category union all
                select ''act'', activity
              ) c (col, value)
            ) x
            pivot 
            (
                max(value)
                for col in (' + @cols + ')
            ) p '

execute sp_executesql @query;

请参阅SQL Fiddle with Demo。所有版本都会给出一个结果:

|    PERSON | CAT1 | CAT1_ACT | CAT2 | CAT2_ACT | CAT3 | CAT3_ACT |
| Username1 |    X |       X1 |    Y |       Y1 |    Z |       Z1 |
于 2013-08-29T15:46:59.900 回答
3

这是一个简单的例子

 SELECT 
       Person,  
       MAX(CASE Category WHEN 'X' THEN Activity ELSE 0 END) AS 'X'
       MAX(CASE Category WHEN 'Y' THEN Activity ELSE 0 END) AS 'Y'
       MAX(CASE Category WHEN 'Z' THEN Activity ELSE 0 END) AS 'Z' 
    FROM mytable
    GROUP BY Person
于 2013-08-29T15:46:39.350 回答