here is my attendance table details
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
A : Absent
WO:Week off
H :Holiday
i have other employee data who is having status P(present) also but i need to fetch those who are continuously absent for 7 days without considering weekoff and holiday ......
Will this do?
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.