当重复数据具有不同的密钥时,查询以获取所有ID

时间:2013-01-22 21:42:21

标签: sql sql-server pivot

我有一个类似于此的SQL表:

+-----------------------------------------------+
|   ID   | FirstName | LastName  | SomeOtherData|
+-----------------------------------------------+
|  200   | Robert    | Barone    | Foo          |
|  228   | Doug      | Heffernan | Bar          |
|  2091  | Robert    | Barone    | Foo          |
|  3921  | Doug      | Heffernan | Bar          |
|  291   | Greg      | Warner    | Barfoo       |
+-----------------------------------------------+

现在我生成的问题是一个表,它列出了给定Person的两个ID,假设FirstName和LastName用于表示重复。所以,基本上我试图得到:

+---------------------------------------------------------+
|   ID   | OtherID | FirstName | LastName  | SomeOtherData|
+---------------------------------------------------------+
|  200   | 2091    | Robert    | Barone    | Foo          |
|  228   | 3921    | Doug      | Heffernan | Bar          |
|  291   |         | Greg      | Warner    | Barfoo       |
+---------------------------------------------------------+

有人能用这样的东西帮助我吗?谢谢!

1 个答案:

答案 0 :(得分:2)

您可以使用PIVOT将数据从行转换为列:

select [1] Id,
  [2] OtherId,
  firstname, 
  lastname
from
(
  select id, firstname, lastname,
    row_number() over(partition by firstname, lastname
                      order by id) rn
  from yourtable
) src
pivot
(
  max(id)
  for rn in ([1], [2])
) piv

请参阅SQL Fiddle with Demo

或者您可以使用带有CASE表达式的聚合函数:

select 
  max(case when rn = 1 then id end) Id,
  max(case when rn = 2 then id end) OtherId,
  firstname,
  lastname
from
(
  select id, firstname, lastname,
    row_number() over(partition by firstname, lastname
                      order by id) rn
  from yourtable
) src
group by firstname, lastname

如果你有一个已知数量的重复值(1,2等),上面的工作会很好。如果你有超过2个id,你也可以实现动态SQL。动态SQL看起来像:

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

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(cast(row_number() over(partition by firstname, lastname order by id) as varchar(50))) 
                    from yourtable
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')


select @colNames = STUFF((SELECT distinct ', ' + QUOTENAME(cast(row_number() over(partition by firstname, lastname order by id) as varchar(50))) +' as Id_' + cast(row_number() over(partition by firstname, lastname order by id) as varchar(50)) 
                    from yourtable
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT ' + @colNames + ', firstname, lastname from 
             (
                select id, firstname, lastname,
                  row_number() over(partition by firstname, lastname
                                    order by id) rn
                from yourtable
            ) x
            pivot 
            (
                max(id)
                for rn in (' + @cols + ')
            ) p 
            '

execute(@query)

请参阅SQL Fiddle with Demo

所有3的结果将是:

|  ID | OTHERID | FIRSTNAME |  LASTNAME |
-----------------------------------------
| 200 |    2091 |    Robert |    Barone |
| 228 |    3921 |      Doug | Heffernan |
| 291 |  (null) |      Greg |    Warner |