1

我的 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

但它的分组不好,我不知道如何做到这一点,任何人都可以在这里帮助我,提前谢谢

4

1 回答 1

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
于 2012-05-19T06:41:44.083 回答