sql case when 较复杂的用法

本文介绍了一个复杂的SQL查询案例,该查询旨在统计员工每日上下班情况,并根据特定条件计算出全天和半天的出勤次数。通过多层CASE WHEN结构和聚合函数实现业务需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

select emp_id,
biz_date,
case when
sum(case when morning =1 and afternoon =1
then 1
else 0
end
)
+
max(case when morning =1 and afternoon =0
then 1
else 0
end
)
+
max(case when morning = 0 and afternoon =1
then 1
else 0
end
)
>=2
then 2
else
sum(case when morning =1 and afternoon =1
then 1
else 0
end)
+
max(case when morning =1 and afternoon =0
then 1
else 0
end)
+
max(case when morning = 0 and afternoon =1
then 1
else 0
end)
end as vsttimes
from
(
SELECT
emp_id,
biz_date,
sr_id,
max(case when convert(varchar(2),intime,108) < 12
then 1
else 0
end) as morning,

max(case when convert(varchar(2),intime,108) >= 12
then 1
else 0
end) as afternoon

FROM RPT_INOUT_STORE WITH (nolock)
WHERE FUNC_CODE='F50S01'
AND BIZ_DATE>= @current_year+'-01-01'
AND BIZ_DATE<= @current_year+'-12-31'
GROUP BY EMP_ID,biz_date,sr_id

) a
group by a.emp_id,a.biz_date
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值