0

好的,我有下表。

Name    ID  Website

Aaron | 2305 |  CoolSave1


Aaron | 8464 |  DiscoWorld1


Adriana |   2956 |  NewCin1


Adriana |   5991 |  NewCin2


Adriana | 4563  NewCin3

我想将其转换为以下方式。

Adriana  |  2956 |  NewCin1 |   5991 |  NewCin2 | 4563  | NewCin3


Aaron | 2305 | CoolSave1 |  8464 |  DiscoWorld | NULL | NULL    

正如您所看到的,我正在尝试从第一个表中获取名字,并使用与该名称关联的所有 ID / 网站创建一行。问题是,可能与每个名称相关联的网站数量不定。为了处理这个问题,我只想制作一个字段数等于最大行项目的表,然后对于后续的行项目,在没有足够数据的地方插入一个 NULL。

4

1 回答 1

5

为了获得结果,您需要将 UNPIVOT 和 PIVOT 函数应用于数据。UNPIVOT 将获取列(ID、网站)并将它们转换为行,一旦完成,您就可以将数据 PIVOT 回列。

UNPIVOT 代码将类似于以下内容:

select name,
  col+'_'+cast(col_num as varchar(10)) col,
  value
from
(
  select name, 
    cast(id as varchar(11)) id, 
    website,
    row_number() over(partition by name order by id) col_num
  from yt
) src
unpivot
(
  value
  for col in (id, website)
) unpiv;

请参阅SQL Fiddle with Demo。这给出了一个结果:

|    NAME |       COL |       VALUE |
-------------------------------------
|   Aaron |      id_1 |        2305 |
|   Aaron | website_1 |   CoolSave1 |
|   Aaron |      id_2 |        8464 |
|   Aaron | website_2 | DiscoWorld1 |

如您所见,我row_number()在取消透视之前将 a 应用于数据,行号用于生成新的列名。UNPIVOT 中的列也必须是相同的数据类型,我将 a 应用于子查询中castid列以将数据转换为数据varchar透视之前的数据。

col然后在 PIVOT 中使用这些值。取消透视数据后,应用 PIVOT 函数:

select *
from
(
  select name,
    col+'_'+cast(col_num as varchar(10)) col,
    value
  from
  (
    select name, 
      cast(id as varchar(11)) id, 
      website,
      row_number() over(partition by name order by id) col_num
    from yt
  ) src
  unpivot
  (
    value
    for col in (id, website)
  ) unpiv
) d
pivot
(
  max(value)
  for col in (id_1, website_1, id_2, website_2, id_3, website_3)
) piv;

请参阅SQL Fiddle with Demo

如果您的值数量有限或已知,则上述版本非常有用。但如果行数未知,则需要使用动态 SQL 生成结果:

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

select @cols = STUFF((SELECT ',' + QUOTENAME( col+'_'+cast(col_num as varchar(10))) 
                    from
                    (
                      select row_number() over(partition by name order by id) col_num
                      from yt
                    ) t
                    cross apply
                    (
                      select 'id' col union all
                      select 'website'
                    ) c
                    group by col, col_num
                    order by col_num, col
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT name,' + @cols + ' 
            from 
            (
               select name,
                col+''_''+cast(col_num as varchar(10)) col,
                value
              from
              (
                select name, 
                  cast(id as varchar(11)) id, 
                  website,
                  row_number() over(partition by name order by id) col_num
                from yt
              ) src
              unpivot
              (
                value
                for col in (id, website)
              ) unpiv
            ) x
            pivot 
            (
                max(value)
                for col in (' + @cols + ')
            ) p '

execute(@query);

请参阅SQL Fiddle with Demo。两个版本都给出了结果:

|    NAME | ID_1 | WEBSITE_1 | ID_2 |   WEBSITE_2 |   ID_3 | WEBSITE_3 |
------------------------------------------------------------------------
|   Aaron | 2305 | CoolSave1 | 8464 | DiscoWorld1 | (null) |    (null) |
| Adriana | 2956 |   NewCin1 | 4563 |     NewCin3 |   5991 |   NewCin2 |
于 2013-04-10T17:08:35.427 回答