这是下面的出勤记录表。
Code Name Date day1 Status
------- ------ --------- -------- -------------------
724 Ramesh 1-May-14 Thursday Present
724 Ramesh 2-May-14 Friday Present
724 Ramesh 3-May-14 Saturday ½Present On Leave(½CL)
724 Ramesh 4-May-14 Sunday WeeklyOff
724 Ramesh 5-May-14 Monday ½Present On Leave(½CL)
724 Ramesh 6-May-14 Tuesday ½Present On Leave(½CL)
724 Ramesh 7-May-14 Wednesday Present
724 Ramesh 8-May-14 Thursday ½Present
724 Ramesh 9-May-14 Friday Present
724 Ramesh 10-May-14 Saturday Present
724 Ramesh 11-May-14 Sunday WeeklyOff ½Present
724 Ramesh 12-May-14 Monday Present
724 Ramesh 13-May-14 Tuesday Present
724 Ramesh 14-May-14 Wednesday Present
724 Ramesh 15-May-14 Thursday Present
724 Ramesh 16-May-14 Friday Present
724 Ramesh 17-May-14 Saturday Present
724 Ramesh 18-May-14 Sunday WeeklyOff
724 Ramesh 19-May-14 Monday Present
724 Ramesh 20-May-14 Tuesday Present On Leave(½CL)
724 Ramesh 21-May-14 Wednesday ½Present
724 Ramesh 22-May-14 Thursday Absent
724 Ramesh 23-May-14 Friday Present
724 Ramesh 24-May-14 Saturday Present
724 Ramesh 25-May-14 Sunday WeeklyOff
724 Ramesh 26-May-14 Monday Present
724 Ramesh 27-May-14 Tuesday Present
724 Ramesh 28-May-14 Wednesday Present
724 Ramesh 29-May-14 Thursday Present
724 Ramesh 30-May-14 Friday Present
724 Ramesh 31-May-14 Saturday Present
预期输出为:
EmployeeCode EmployeeName Monday Saturday TotalLeaves
------------- -------------- ------- ---------- ------------
724 Ramesh 0.5 0.5 4(days)
我试图根据状态栏
获取一个月内周一和周六的假期数我的查询是
select EmployeeCode
,EmployeeName
,AttendanceDate
,datename(dw,AttendanceDate) as 'day1'
,InTime
,a.PunchRecords
,a.Status
from AttendanceLogs a
join Employees b on a.EmployeeId=b.EmployeeId
where AttendanceDate >='2014-05-01'
and AttendanceDate <='2014-05-31'
and EmployeeCode='724'
and datename(dw,AttendanceDate) IN('Monday','Saturday')
and a.Status!='present'
order by AttendanceDate
请帮助我获得预期的输出。
答案 0 :(得分:0)
我猜是这样的:
WITH cte AS (
SELECT code ,
name ,
[date] ,
CASE WHEN [status] LIKE '1/2Present%' THEN 0.5
WHEN [status] = 'Absent' THEN 1
ELSE 0
END AS StatusAdder
FROM tablename
WHERE attendancedate >= '2014-05-01'
AND attendancedate <= '2014-05-31'
AND code = '724'
)
SELECT code ,
name ,
CASE WHEN DATEPART(WEEKDAY, date) = 2 THEN SUM(statusAdder) END AS Monday ,
CASE WHEN DATEPART(WEEKDAY, date) = 7 THEN SUM(statusAdder) END AS Saturday ,
SUM(statusAdder) AS TotalLeaves
FROM cte
GROUP BY code ,
name
您必须使用对象名称进行调整。