如何在一个“重复ID”中合并两行的值。

时间:2017-10-25 17:23:17

标签: sql sql-server tsql join

我试图找到答案,但无论我发现什么都不适用于我的情况,所以我将不胜感激任何帮助:

场景:我在一个表格中拥有ID和个人信息的客户,然后我有另一个表格,表明属于这些ID的笔记。 当我加入表格时,我会在乘法行中得到多个音符。如下图所示。

T1. ID -T1. Name    -T1. Birthday   -T2. Note                  -Row number
2      -Peter       -11/20/1990     -deciding                  -1
2      -Peter       -11/20/1990     -purchased                 -2
3      -David       -12/22/1962     -Presentation scheduled    -1  
3      -David       -12/22/1962     -Presentation completed    -2
3      -David       -12/22/1962     -Purchased                 -3
4      -Anna        -5/07/1992      -Ignored                   -1

我在徘徊的是如何在第一行末尾添加注释2和3并删除重复项?

T1. ID -T1. Name    -T1. Birthday   -T2. Note               -Note_2      -Note_3
2      -Peter       -11/20/1990     -deciding               -purchased  
3      -David       -12/22/1962     -Presentation scheduled -Presentation -completed    Purchased
4      -Anna            -5/07/1992  -Ignored                -Null        -Null 

您可以使用此查询查看表格:

declare  @t1  table ( ID int not null,
                      Name varchar (50),
                      birthday date)

declare @t2 table (ID int not null,
                    Note nvarchar(max))


insert into @t1 values ( 2 , 'Peter' , '11/20/1990')
insert into @t1 values ( 3 , 'David' ,'12/22/1962')
insert into @t1 values ( 4 , 'Anna' , '5/07/1992')

insert into @t2 values (2 , 'deciding')
insert into @t2 values (2 ,'purchased')
insert into @t2 values (3 ,'Presentation scheduled')
insert into @t2 values (3 ,'Presentation completed')
insert into @t2 values (3 ,'Purchased')
insert into @t2 values (4 ,'Ignored')



SELECT * 
        , ROW_NUMBER() OVER (PARTITION BY t1.ID ORDER BY t2.id) AS 'ROW NUMBER'
FROM @t1 as t1
left outer join @t2 AS t2 ON t1.ID = t2.ID 

2 个答案:

答案 0 :(得分:1)

我在这里找到了这个CLR解决方案: https://groupconcat.codeplex.com 并成功地利用它。

基本上,您的查询将变为:

ID  name    birthday    Notes
2   Peter   1990-11-20  deciding   -purchased
3   David   1962-12-22  Presentation scheduled   -Presentation completed   -Purchased
4   Anna    1992-05-07  Ignored

结果如下:

showCirculares

答案 1 :(得分:1)

这是一个完整的脚本。我知道它可能不像上面的那样简洁,但它没有任何额外的附加功能,也很容易解释。

我唯一不确定的是你是想要三个单独的音符列还是一个带有连接音符的列。我使用了连接注释方法......

declare @t1 table
(
    ID int not null,
    Name varchar(50),
    birthday date
)
declare @t2 table
(
    ID int not null,
    Note nvarchar(max)
)
insert into @t1
values
(2, 'Peter', '11/20/1990')
insert into @t1
values
(3, 'David', '12/22/1962')
insert into @t1
values
(4, 'Anna', '5/07/1992')
insert into @t2
values
(2, 'deciding')
insert into @t2
values
(2, 'purchased')
insert into @t2
values
(3, 'Presentation scheduled')
insert into @t2
values
(3, 'Presentation completed')
insert into @t2
values
(3, 'Purchased')
insert into @t2
values
(4, 'Ignored')

--Create table of all notes and what person they belong to.
declare @Notes table
(
    NoteID int not null,
    ID int not null,
    Note nvarchar(max)
)

--Insert into note table
insert into @Notes
(
    NoteID,
    ID,
    Note
)
SELECT ROW_NUMBER() OVER (ORDER BY t2.note),
       t1.ID,
       Note
FROM @t1 as t1
    left outer join @t2 AS t2
        ON t1.ID = t2.ID

--Create a table of person ID and concatenated notes.
declare @ConcatNotes table
(
    ID int not null,
    Note nvarchar(max)
)

--Insert just people first
insert into @ConcatNotes
(
    ID
)
select distinct
    ID
FROM @Notes

declare @NoteCount int
declare @i int = 1

select @NoteCount = max(noteid)
from @Notes

--While loop to loop through ALL notes by person ID and concatenate with semicolon. Change semicolon to whatever you want.
while (@i <= @NoteCount)
BEGIN
    update a
    set Note = isnull(a.Note + ';', '') + b.Note
    --select *
    from @ConcatNotes a
        inner join @Notes b
            on a.id = b.ID
    where b.Noteid = @i
    set @i = @i + 1
END

--A select to show the person ID and the concatenated notes.
select *
from @ConcatNotes

--The final query you were looking for.
select a.*,
       b.Note
from @t1 a
    inner join @ConcatNotes b
        on a.ID = b.ID