按给定字段对多个 SQL 结果进行分组
Grouping multile SQL results by a given field
如何按员工对以下查询的结果进行分组?
好的,所以我有三个表:
1. company_employee 其中包含映射到公司 ID (company.id) 的员工 ID (company_employee.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)
这可能吗?
您似乎在寻找 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
如何按员工对以下查询的结果进行分组?
好的,所以我有三个表: 1. company_employee 其中包含映射到公司 ID (company.id) 的员工 ID (company_employee.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)
这可能吗?
您似乎在寻找 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