我有一个包含如下行
的表格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
答案 0 :(得分:27)
您使用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;
在Oracle 11g +中,您可以使用LISTAGG
:
select "User",
listagg(department, ',') within group (order by "User") as departments
from yourtable
group by "User"
在Oracle 11g之前,您可以使用wm_concat
函数:
select "User",
wm_concat(department) departments
from yourtable
group by "User"
答案 1 :(得分:6)
在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
答案 2 :(得分:4)
MYSQL:要将列值作为逗号分隔值使用GROUP_CONCAT( )
函数作为
GROUP_CONCAT( `column_name` )
例如
SELECT GROUP_CONCAT( `column_name` )
FROM `table_name`
WHERE 1
LIMIT 0 , 30
答案 3 :(得分:3)
您可以使用以下SQL执行此操作:
SELECT STUFF
(
(
SELECT ',' + s.FirstName
FROM Employee s
ORDER BY s.FirstName FOR XML PATH('')
),
1, 1, ''
) AS Employees
答案 4 :(得分: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
答案 5 :(得分:1)
SELECT name, GROUP_CONCAT( section )
FROM `tmp`
GROUP BY name
答案 6 :(得分:1)
I think it will be easy to you. I am using group_concat which concatenate diffent values with separator as we have defined
select ID,User, GROUP_CONCAT(Distinct Department order by Department asc
separator ', ') as Department from Table_Name group by ID
答案 7 :(得分:1)
尝试以下查询:
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
答案 8 :(得分:0)
对于不支持WM_CONCAT的Oracle版本,可以使用以下内容
select "User", RTRIM(
XMLAGG (XMLELEMENT(e, department||',') ORDER BY department).EXTRACT('//text()') , ','
) AS departments
from yourtable
group by "User"
这个功能更加强大和灵活 - 您可以在 listagg 中指定每个组中的分隔符和排序顺序。