如何按员工对以下查询的结果进行分组?
好的,所以我有三张桌子: 1. company_employee,其中包含映射到公司ID(company.id)的员工ID(company_employee.employee)。它还包含员工角色
员工,其中包含员工名,姓氏
包含ID和名称
现在我正在检索具有某个角色的所有员工,但是我为同一个员工获得了多个具有不同公司映射的条目。这样的事情: 我的疑问是:
SELECT company_employee.employee, company_employee.company, employee.fname, employee.lname, company.name
FROM company_employee
JOIN employee on employee.id = company_employee.employee
JOIN company on company.id = company_employee.company
WHERE company_employee.role= 185;
我的结果是:
company_employee.employee company_employee.company employee.fname employee.lname company.name
111 100 John Smith Super Candy
111 101 John Smith Red Ballons
222 102 Kevin Lora Super Computers
111 103 John Smith Star Events
222 104 Kevin Lora Vintage Pencils
333 105 Margarett Bush Top Security
我想得到的是这种类型的清单:
Employee 111 John Smith mapped to a list of companies (100, 101, 103)
Employee 222 Kevin Lora mapped to a list of companies (102, 104)
Employee 333 Margarett Bush mapped to a list of companies (105)
这可能吗?
答案 0 :(得分:0)
好像您正在寻找listagg
功能:
SELECT company_employee.employee, employee.fname, employee.lname,
LISTAGG(company_employee.company)
WITHIN GROUP (ORDER BY company_employee.company),
LISTAGG(company.name)
WITHIN GROUP (ORDER BY company_employee.company),
FROM company_employee
JOIN employee ON employee.id = company_employee.employee
JOIN company ON company.id = company_employee.company
WHERE company_employee.role = 185
GROUP BY company_employee.employee, employee.fname, employee.lname