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.
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:
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
:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.