21

我有一个包含如下行的表

ID  User      Department
1   User1     Admin
2   User1     Accounts
3   User2     Finance
4   User3     Sales
5   User3     Finance

我需要一个选择查询,其结果如下格式

ID  User      Department
1   User1     Admin,Accounts
2   User2     Finance
3   User3     Sales, Finance
4

10 回答 10

36

你用 sql-server 和 plsql 标记了这个问题,所以我将为 SQL Server 和 Oracle 提供答案。

在 SQL Server 中,您可以使用FOR XML PATH将多行连接在一起:

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;

请参阅SQL Fiddle with Demo

在 Oracle 11g+ 中,您可以使用LISTAGG

select "User",
  listagg(department, ',') within group (order by "User") as departments
from yourtable
group by "User"

请参阅带有演示的 SQL Fiddle

在 Oracle 11g 之前,您可以使用以下wm_concat功能:

select "User",
  wm_concat(department) departments
from yourtable
group by "User"
于 2013-04-01T11:47:35.943 回答
12

在 Sql Server 中你可以使用它。

DECLARE @UserMaster TABLE( 

    UserID INT NOT NULL, 

    UserName varchar(30) NOT NULL 

); 

INSERT INTO @UserMaster VALUES (1,'Rakesh')

INSERT INTO @UserMaster VALUES (2,'Ashish')

INSERT INTO @UserMaster VALUES (3,'Sagar')

SELECT * FROM @UserMaster

DECLARE @CSV VARCHAR(MAX) 

SELECT @CSV = COALESCE(@CSV + ', ', '') + UserName from @UserMaster 

SELECT @CSV AS Result
于 2013-11-21T09:17:58.637 回答
8

您可以使用以下 SQL 执行此操作:

SELECT STUFF
(
    (
        SELECT ',' + s.FirstName 
        FROM Employee s
        ORDER BY s.FirstName FOR XML PATH('')
    ),
     1, 1, ''
) AS Employees
于 2015-12-16T08:39:59.863 回答
6

MYSQL:要将列值作为一个逗号分隔值使用 GROUP_CONCAT( )函数作为

GROUP_CONCAT(  `column_name` )

例如

SELECT GROUP_CONCAT(  `column_name` ) 
FROM  `table_name` 
WHERE 1 
LIMIT 0 , 30
于 2013-09-27T07:01:19.277 回答
2

尝试以下查询:

select distinct Users,
 STUFF(
        (
        select ', ' + d.Department FROM @temp d
        where t.Users=d.Users
group by d.Department for xml path('')
        ), 1, 2, '') as Departments
 from @temp t

执行:

Declare @temp Table(
ID int,
Users varchar(50),
Department varchar(50)
)
insert into @temp
(ID,Users,Department)
values
(1,'User1','Admin')
insert into @temp
(ID,Users,Department)
values
(2,'User1','Accounts')
insert into @temp
(ID,Users,Department)
values
(3,'User2','Finance')
insert into @temp
(ID,Users,Department)
values
(4,'User3','Sales')
insert into @temp
(ID,Users,Department)
values
(5,'User3','Finance')

 select distinct Users,
 STUFF(
        (
        select ', ' + d.Department FROM @temp d
        where t.Users=d.Users
group by d.Department for xml path('')
        ), 1, 2, '') as Departments

 from @temp t

结果将是:

在此处输入图像描述

于 2017-08-29T09:57:12.487 回答
1

对于 Mysql:

SELECT t.user, 

(SELECT GROUP_CONCAT( t1.department ) FROM table_name t1 WHERE t1.user = t.user)department

FROM table_name t
GROUP BY t.user
LIMIT 0 , 30
于 2014-07-10T11:50:19.457 回答
1
SELECT name, GROUP_CONCAT( section ) 
FROM  `tmp` 
GROUP BY name
于 2015-02-04T06:30:25.773 回答
1

我想这对你来说很容易。我正在使用 group_concat 将不同的值与我们定义的分隔符连接起来

select ID,User, GROUP_CONCAT(Distinct Department order  by Department asc 

separator ', ') as Department from Table_Name   group by ID
于 2017-08-13T13:49:05.123 回答
0

对于不支持 WM_CONCAT 的 Oracle 版本,可以使用以下

  select "User", RTRIM(
     XMLAGG (XMLELEMENT(e, department||',') ORDER BY department).EXTRACT('//text()') , ','
     ) AS departments 
  from yourtable 
  group by "User"

这个功能更加强大和灵活——您可以像在listagg中一样在每个组中指定分隔符和排序顺序。

于 2014-03-25T07:10:44.870 回答
0
DECLARE @UserMaster TABLE(
    UserID INT NOT NULL,    
    UserName varchar(30) NOT NULL,
    Department varchar(30) NOT NULL
);



INSERT INTO @UserMaster VALUES (1,'User1', 'Admin')
INSERT INTO @UserMaster VALUES (2,'User1', 'Accounts')
INSERT INTO @UserMaster VALUES (3,'User2', 'Finance')
INSERT INTO @UserMaster VALUES (1,'User3', 'Sales')
INSERT INTO @UserMaster VALUES (2,'User3', 'Finance')

SELECT UserName, String_Agg(Department, ',') as Department 
FROM @UserMaster
group by UserName
于 2021-10-28T08:54:34.997 回答