Du taggede spørgsmålet med både sql-server og plsql, så jeg vil give svar til både SQL Server og Oracle.
I SQL Server kan du bruge FOR XML PATH
for at sammenkæde flere rækker sammen:
select distinct t.[user],
STUFF((SELECT distinct ', ' + t1.department
from yourtable t1
where t.[user] = t1.[user]
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,2,'') department
from yourtable t;
Se SQL Fiddle with Demo .
I Oracle 11g+ kan du bruge LISTAGG
:
select "User",
listagg(department, ',') within group (order by "User") as departments
from yourtable
group by "User"
Før Oracle 11g kunne du bruge wm_concat
funktion:
select "User",
wm_concat(department) departments
from yourtable
group by "User"