我想做这样的事情。
select
EmployeeID e,
SupervisorID s
from
ReportingStructure
where
count(select SupervisorID from ReportingStructure where EmployeeID = e) > 1
找出所有拥有多个主管的员工的ID和主管ID。但我得到一个语法错误。
“select “关键字附近的语法错误。
‘)’附近的语法不正确。
我怎样才能得到这些数据?
解决方案:
你可以使用 exists
:
select EmployeeID, SupervisorID
from ReportingStructure rs
where exists (select 1
from ReportingStructure rs2
where rs2.EmployeeID = rs.EmployeeID and
rs2.SupervisorID <> rs.SupervisorID
);
如果你想让上级在一排,你可以使用 string_agg()
:
select EmployeeID, string_agg(SupervisorID, ',')
from ReportingStructure
group by EmployeeID
having count(*) = 2;