我的sql表中有两个bit
类型的列用于员工出勤,以及1 for Medical Leave or 2 for Paid Leave
之类的休假类型,其中的值类似,
FirstHalfLeave Type SecondHalfLeave Type
------------ ---- ------------- ----
0(Absent) 1 1 2
1(Present) 1 1 2
我需要将这两个字段相加以使其离开一天,我需要显示确切的结果,如
Type Days
---- ------
MedicalLeave 0.5
PaidLeave 1.0
为此我尝试了
select cast(
sum(
(
CAST(StaffAttendance.FirstHalfStatus as Integer) +
CAST(StaffAttendance.SecondHalfStatus as integer)
)
/ 2.0
) as float
) as TotalLeave
,case when StaffAttendance.FirstHalfLeaveType = 2 OR
StaffAttendance.SecondHalfLeaveType=2 then 'Paid Leave'
else 'Medical Leave'
end as Status
from StaffAttendance
where MONTH(StaffAttendance.date)=MONTH(GetDate())
group by StaffAttendance.FirstHalfLeaveType,StaffAttendance.SecondHalfLeaveType
但它没有分组,我不知道如何做到这一点,任何人都可以帮助我,在此先感谢
答案 0 :(得分:2)
SELECT SUM(sa.LeaveDays)/COUNT(sa.LeaveDays),sa.Status
FROM
(
SELECT CAST(FirstHalfStatus AS FlOAT) LeaveDays,CASE WHEN FirstHalfLeaveType=2 THEN 'Paid Leave' ELSE 'Medical Leave' END as Status,date
FROM StaffAttendance
UNION ALL
SELECT CAST(SecondHalfStatus AS FlOAT),CASE WHEN SecondHalfLeaveType=2 THEN 'Paid Leave' ELSE 'Medical Leave' END as Status,date
FROM StaffAttendance
) AS sa
WHERE MONTH(sa.date)=MONTH(GetDate())
GROUP BY sa.Status