[英]Rolling up multiple rows into a single row
Need a query to get the name of the project and their respective employee first name who have a project assigned. 需要查询以获取项目名称以及分配了项目的员工各自的名字。 If more than one employee is under a given project, then second column should have all the names separated by a comma. 如果给定项目下有多个员工,则第二列应将所有名称用逗号分隔。 Example: Vikash, Ashish. 例如:Vikash,Ashish。
I have two tables: 我有两个表:
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
I have used the following code to get employees name and project they are working on: 我使用以下代码获取员工的姓名和他们正在从事的项目:
select FirstName, ProjectName
from EmployeeInfo, EmployeeProjects
where EmployeeID = EmployeeDetailID;
OUTPUT: OUTPUT:
Vikas Task Track
Vikas CLP
Vikas Survey Managment
nikita HR Managment
Ashish Task Track
Ashish GRS
Ashish DDS
Nikhil HR Managment
I don`t know how combine multiple rows and separate them using comma. 我不知道如何组合多个行并使用逗号分隔它们。
perhaps use a concatenation function that operates on a group like listagg
: 也许使用对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
Try this: 尝试这个:
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;
LISTAGG
will throw an error if your consolidated string becomes more than 4000 characters long. 如果您的合并字符串长度超过4000个字符,则LISTAGG
将引发错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.