使用SQL Server 2012.尝试根据两个不同工作流程/条件的员工ID获取计数。最终,希望有三列; EmpID,计数方法1,计数方法2.尝试了几个子查询,但我被卡住了。
-- Returns count for workflow 1
select
s.id [Employee ID], count(r.requisitionnumber)[Method 1]
from
requisition r
left outer join
staff s on r.placerkey = s.staffkey
left outer join
location l on r.placerSignedInLocationKey = l.locationkey
group by
s.id
-- Returns count for workflow 2
select distinct
s.id [Employee ID], count(distinct(r.requisitionnumber) [Method 2]
from
requisition r
left outer join
orderedpanel op on r.requisitionkey = op.requisitionkey
left outer join
staff s on s.staffkey = op.staffkeyreleasedorderchoices
and staffKeyReleasedOrderChoices > 1
group by
s.id
答案 0 :(得分:0)
您始终可以使用联接来连接两个不同的查询:
select
[Employee ID],
[Method 1],
[Method 2]
from (
--Gives count for workflow 1
select s.id [Employee ID], count(r.requisitionnumber)[Method 1]
from requisition r
left outer join staff s
on r.placerkey=s.staffkey
left outer join location l on r.placerSignedInLocationKey=l.locationkey
group by s.id
) workflow1
inner join (
--Gives count for workflow 2
select distinct s.id [Employee ID],count(distinct(r.requisitionnumber) [Method 2]
from requisition r
left outer join orderedpanel op
on r.requisitionkey=op.requisitionkey
left outer join staff s on s.staffkey=op.staffkeyreleasedorderchoices
where staffKeyReleasedOrderChoices >1
group by s.id
) workflow2 on workflow1.[Employee ID] = workflow2.[Employee ID]