我有一个MySQL表ATT
,其EMP_ID
,ATT_DATE
,ATT_STATUS
ATT_STATUS
,EMP_ID
值不同1-Present,2-Absent,3 -Weekly断。我想找出那些在给定日期范围内连续10天具有状态2的{{1}}个。
请帮忙
答案 0 :(得分:1)
请试试这个:
SELECT EMP_ID FROM (
SELECT
IF((@prevDate!=(q.ATT_DATE - INTERVAL 1 DAY)) OR (@prevEmp!=q.EMP_ID) OR (q.ATT_STATUS != 2), @rownum:=@rownum+1, @rownum:=@rownum) AS rownumber, @prevDate:=q.ATT_DATE, @prevEmp:=q.EMP_ID, q.*
FROM (
SELECT
EMP_ID
, ATT_DATE
, ATT_STATUS
FROM
org_tb_dailyattendance, (SELECT @rownum:=0, @prevDate:='', @prevEmp:=0) vars
WHERE ATT_DATE BETWEEN '2013-01-01' AND '2013-02-15'
ORDER BY EMP_ID, ATT_DATE, ATT_STATUS
) q
) sq
GROUP BY EMP_ID, rownumber
HAVING COUNT(*) >= 10
逻辑是,首先按员工ID和日期对表进行排序。然后介绍一个仅在
时增加的rownumber然后我只按这个rownumber分组并计算每组中是否有10行。那应该是那些缺席10天或更长时间的人。
答案 1 :(得分:-1)
你有没有试过这样的东西
SELECT EMP_ID count(*) as consecutive_count min(ATT_DATE)
FROM (SELECT * FROM ATT ORDER BY EMP_ID)
GROUP BY EMP_ID, ATT_DATE
WHERE ATT_STATUS = 2
HAVING consecutive_count > 10