需要查询以获取项目名称以及分配了项目的员工各自的名字。如果在给定的项目下有多个员工,则第二列应将所有名称用 逗号。例如:Vikash,Ashish。
我有两个表:
Table1: EmployeeID, FirstName
1 Vikas
2 nikita
3 Ashish
4 Nikhil
5 anish
Table2: EmployeeDetailID, ProjectName
1|Task Track
1|CLP
1|Survey Managment
2|HR Managment
3|Task Track
3|GRS
3|DDS
4|HR Managment
6|GL Managment
我使用以下代码获取员工的姓名和他们正在从事的项目:
select FirstName, ProjectName
from EmployeeInfo, EmployeeProjects
where EmployeeID = EmployeeDetailID;
输出:
Vikas Task Track
Vikas CLP
Vikas Survey Managment
nikita HR Managment
Ashish Task Track
Ashish GRS
Ashish DDS
Nikhil HR Managment
我不知道如何组合多行并使用逗号分隔它们。
答案 0 :(得分:1)
也许使用在listagg
之类的组上运行的串联功能:
select Table2.ProjectName, listagg(Table1.FirstName, ', ')
within group (order by Table1.FirstName) as members
from Table1, Table2
where Table1.EmployeeID = Table2.EmployeeDetailID
group by Table2.ProjectName
答案 1 :(得分:0)
尝试一下:
SELECT
T2.PROJECTNAME,
RTRIM(XMLAGG(XMLELEMENT(E, T1.FIRSTNAME, ',').EXTRACT('//text()')
ORDER BY
T1.FIRSTNAME
).GETCLOBVAL(), ',') AS MEMBERS
FROM
TABLE1 T1
JOIN TABLE2 T2 ON ( T1.EMPLOYEEID = T2.EMPLOYEEDETAILID )
GROUP BY
T2.PROJECTNAME;
如果您的合并字符串的长度超过4000个字符, LISTAGG
将引发错误。