我正在使用SQL Server 2008 R2,我想显示可以申请,批准,拒绝或取消的申请状态。在SQL Server中允许审批状态
如果全部离开批准则状态=批准,像其他叶子
但如果有混合状态e.g一些离开批准,那么一些拒收状态=部分。
我写了代码,但我觉得它很复杂,我可以在一个单一的查询中得到它吗?
create table #t
(
employeeID int,
LeaveCode nvarchar(10),
status nvarchar(50)
)
insert into #t
values(1, 'PL', 'Approve'), (1, 'PL', 'Reject'), (1, 'PL', 'Approve')
;with ct1 as
(
select status, count(status) Cnt
from #t
group by status
),
counters as
(
select count(*) as TotalLeave
from #t
)
select top(1)
CASE
WHEN C1.Cnt = C2.TotalLeave
THEN C1.status
ELSE 'Partial'
END [status]
from
ct1 C1
cross join
counters C2
drop table #t