转置没有聚合的行和列

use*_*440 5 sql-server pivot sql-server-2008

我有以下数据集

Account Contact

1   324324324
1   674323234
2   833343432
2   433243443
3   787655455
4   754327545
4   455435435
5   543544355
5   432455553
5   432433242
5   432432432
Run Code Online (Sandbox Code Playgroud)

我想输出如下:

Account Contact1    Contact2    Contact3    Contact4

1   324324324   674323234       
2   833343432   433243443       
3   787655455           
4   754327545   455435435       
5   543544355   432455553   432433242   432432432
Run Code Online (Sandbox Code Playgroud)

问题还在于我有一个不固定的帐户数量和未固定数量的联系人

Tar*_*ryn 9

如果要应用该PIVOT函数,则需要使用聚合函数来获取结果,但是您还需要使用窗口函数row_number()来为帐户中的每个联系人生成唯一的序列.

首先,您将查询类似于以下内容的数据:

select account, contact,
  'contact'
    + cast(row_number() over(partition by account
                              order by contact) as varchar(10)) seq
from yourtable
Run Code Online (Sandbox Code Playgroud)

请参阅SQL Fiddle with Demo.这将创建一个具有唯一序列的新列:

| ACCOUNT |   CONTACT |      SEQ |
|---------|-----------|----------|
|       1 | 324324324 | contact1 |
|       1 | 674323234 | contact2 |
Run Code Online (Sandbox Code Playgroud)

如果您的列数有限,则可以对查询进行硬编码:

select account,
  contact1, contact2, contact3, contact4
from 
(
  select account, contact,
    'contact'
      + cast(row_number() over(partition by account
                                order by contact) as varchar(10)) seq
  from yourtable
) d
pivot
(
  max(contact)
  for seq in (contact1, contact2, contact3, contact4)
) piv;
Run Code Online (Sandbox Code Playgroud)

请参阅SQL Fiddle with Demo

如果您的列数未知,则必须使用动态SQL:

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

select @cols = STUFF((SELECT ',' + QUOTENAME(seq) 
                    from
                    (
                      select 'contact'
                              + cast(row_number() over(partition by account
                                                        order by contact) as varchar(10)) seq
                      from yourtable
                    ) d
                    group by seq
                    order by seq
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT account, ' + @cols + ' 
            from 
            (
                select account, contact,
                  ''contact''
                    + cast(row_number() over(partition by account
                                              order by contact) as varchar(10)) seq
                from yourtable
            ) x
            pivot 
            (
                max(contact)
                for seq in (' + @cols + ')
            ) p '

execute sp_executesql @query;
Run Code Online (Sandbox Code Playgroud)

请参阅SQL Fiddle with Demo.两者都会给你一个结果:

| ACCOUNT |  CONTACT1 |  CONTACT2 |  CONTACT3 |  CONTACT4 |
|---------|-----------|-----------|-----------|-----------|
|       1 | 324324324 | 674323234 |    (null) |    (null) |
|       2 | 433243443 | 833343432 |    (null) |    (null) |
|       3 | 787655455 |    (null) |    (null) |    (null) |
|       4 | 455435435 | 754327545 |    (null) |    (null) |
|       5 | 432432432 | 432433242 | 432455553 | 543544355 |
Run Code Online (Sandbox Code Playgroud)