1

我有一个类似这样的 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       |
+-----------------------------------------------+

现在我在生成一个表格时遇到了麻烦,该表格将列出给定人员的两个 ID,假设 FirstName 和 LastName 用于指示重复项。所以,基本上我想得到:

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

有人能帮我解决这样的问题吗?谢谢!

4

1 回答 1

2

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

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

或者您可以使用带有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

所有 3 的结果将是:

|  ID | OTHERID | FIRSTNAME |  LASTNAME |
-----------------------------------------
| 200 |    2091 |    Robert |    Barone |
| 228 |    3921 |      Doug | Heffernan |
| 291 |  (null) |      Greg |    Warner |
于 2013-01-22T21:46:12.373 回答