SQL Server How to concatenate many rows with same id in sql?

xv8emn3q  于 2023-03-22  发布在  其他
关注(0)|答案(8)|浏览(199)

My table contains the details like with two fields:

ID      DisplayName
1        Editor
1        Reviewer
7        EIC
7        Editor
7        Reviewer
7        Editor
19       EIC
19       Editor
19       Reviewer

I want get the unique details with DisplayName like

1 Editor,Reviewer 7 EIC,Editor,Reviewer

Don't get duplicate value with ID 7

How to combine DisplayName Details? How to write the Query?

vkc1a9a2

vkc1a9a21#

In SQL-Server you can do it in the following:

QUERY

SELECT id, displayname = 
    STUFF((SELECT DISTINCT ', ' + displayname
           FROM #t b 
           WHERE b.id = a.id 
          FOR XML PATH('')), 1, 2, '')
FROM #t a
GROUP BY id

TEST DATA

create table #t 
(
id int,
displayname nvarchar(max)
)

insert into #t values    
 (1 ,'Editor')
,(1 ,'Reviewer')
,(7 ,'EIC')
,(7 ,'Editor')
,(7 ,'Reviewer')
,(7 ,'Editor')
,(19,'EIC')
,(19,'Editor')
,(19,'Reviewer')

OUTPUT

id  displayname
1   Editor, Reviewer
7   Editor, EIC, Reviewer
19  Editor, EIC, Reviewer
zpgglvta

zpgglvta2#

DECLARE @t TABLE
(
    ID INT,
    DisplayName VARCHAR(50)
)
INSERT INTO @t (ID, DisplayName)
VALUES
    (1 , 'Editor'),
    (1 , 'Reviewer'),
    (7 , 'EIC'),
    (7 , 'Editor'),
    (7 , 'Reviewer'),
    (7 , 'Editor'),
    (19, 'EIC'),
    (19, 'Editor'),
    (19, 'Reviewer')

SELECT *, STUFF((
            SELECT DISTINCT ', ' + DisplayName
            FROM @t
            WHERE ID = t.ID
            FOR XML PATH('')), 1, 2, '')
FROM (
    SELECT DISTINCT ID
    FROM @t
) t

Output -

----------- ------------------------
1           Editor, Reviewer
7           Editor, EIC, Reviewer
19          Editor, EIC, Reviewer

My post about string aggregation:

http://www.codeproject.com/Articles/691102/String-Aggregation-in-the-World-of-SQL-Server

luaexgnf

luaexgnf3#

For MySQL:
SELECT id, GROUP_CONCAT(displayname) FROM tableName GROUP BY id

Refer: http://www.sqlines.com/mysql/functions/group_concat

xmq68pz9

xmq68pz94#

SQL Server 2017+ and SQL Azure: STRING_AGG

Starting with the next version of SQL Server, we can finally concatenate across rows without having to resort to any variable or XML witchery.

STRING_AGG (Transact-SQL)

SELECT ID, STRING_AGG(DisplayName, ', ') AS DisplayNames
FROM TableName
GROUP BY ID
bn31dyow

bn31dyow5#

and in case of oracle database

select id, 
       listagg(displayname, ',') within group (order by displayname) as names
from test
group by id
a0x5cqrl

a0x5cqrl6#

to change the separator use

SELECT id, GROUP_CONCAT(displayname SEPARATOR ';') FROM tableName GROUP BY id

this will change separator from comma to semicolon :)

vjhs03f7

vjhs03f77#

Thank you all,

SELECT Distinct
    t1.ID,
    MAX(STUFF(t2.x_id,1,1,'')) AS DisplayName
FROM Table t1
CROSS apply(
    SELECT Distinct ', ' + SUBSTRING(t2.DisplayName,1,2)
    FROM Table t2
    WHERE t2.ID = t1.ID AND t2.DisplayName > ''
    FOR xml PATH('')
) AS t2 (x_id)
GROUP BY
    t1.ID
order by 1
GO
4ioopgfo

4ioopgfo8#

In Oracle SQL, you can use LISTAGG .

SELECT ID, LISTAGG(DisplayName, ', ') as all_agg
FROM TABLE_NAME GROUP BY ID;

相关问题