这是我的考勤表详情
Emp_id Emp_name PDate status
000002 Pramod 2014-01-11 A
000002 Pramod 2014-01-12 WO
000002 Pramod 2014-01-13 A
000002 Pramod 2014-01-14 A
000002 Pramod 2014-01-15 H
000002 Pramod 2014-01-16 A
000002 Pramod 2014-01-17 A
000002 Pramod 2014-01-18 A
000002 Pramod 2014-01-19 WO
000002 Pramod 2014-01-20 A
000002 Pramod 2014-01-21 A
Run Code Online (Sandbox Code Playgroud)
答:缺席
WO:休息周
H :假期
我还有其他状态为 P(present) 的员工数据,但我需要获取那些连续缺勤 7 天的员工,而不考虑周末和假期......
这会吗?
CREATE TABLE Attendance(
Emp_id VARCHAR(10),
Emp_name VARCHAR(10),
PDate DATE,
Status VARCHAR(2)
)
INSERT INTO Attendance VALUES
('000002', 'Pramod', '2014-01-11', 'A'),
('000002', 'Pramod', '2014-01-12', 'WO'),
('000002', 'Pramod', '2014-01-13', 'A'),
('000002', 'Pramod', '2014-01-14', 'A'),
('000002', 'Pramod', '2014-01-15', 'H'),
('000002', 'Pramod', '2014-01-16', 'A'),
('000002', 'Pramod', '2014-01-17', 'A'),
('000002', 'Pramod', '2014-01-18', 'A'),
('000002', 'Pramod', '2014-01-19', 'A'),
('000002', 'Pramod', '2014-01-20', 'P'),
('000002', 'Pramod', '2014-01-21', 'A');
;WITH GroupedDates AS(
SELECT
*,
DateGroup = DATEADD(DD, - ROW_NUMBER() OVER (PARTITION BY Emp_id ORDER BY PDate), PDate)
FROM Attendance
WHERE
Status IN('A', 'WO', 'H')
)
SELECT
Emp_id,
Emp_name,
StartDate = MIN(PDate),
EndDate = MAX(PDate),
Days = DATEDIFF(DD, MIN(PDate), MAX(PDate)) + 1
- SUM((CASE WHEN Status IN('WO', 'H') THEN 1 ELSE 0 END))
FROM GroupedDates
GROUP BY
Emp_id, Emp_name, DateGroup
HAVING
SUM(CASE WHEN Status = 'A' THEN 1 ELSE 0 END) >=7
ORDER BY
Emp_id, Emp_name, StartDate
DROP TABLE Attendance
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1468 次 |
| 最近记录: |