我怎么能在sql中做到这一点

时间:2011-11-15 06:26:42

标签: sql subquery

    SELECT Office.OFFICEID
   , Office.OFFICENAME AS OfficeName
   , SUM(ISNULL(AttDet.Present, 0)) AS Present
   , SUM(ISNULL(AttDet.Leave, 0)) AS Leave
   , (
      SELECT COUNT(NewEmpID) AS Expr1
      FROM EMP
      WHERE (ISNULL(DOR, dbo.fn_getdate()) >= '10-01-2010')
         AND (OfficeID = Office.OFFICEID)
      ) - (SUM(ISNULL(AttDet.Present, 0)) + SUM(ISNULL(AttDet.Leave, 0))) AS Absent
   , (
      SELECT COUNT(NewEmpID) AS Expr1
      FROM EMP AS EMP_2
      WHERE (ISNULL(DOR, dbo.fn_getdate()) >= '10-01-2010')
         AND (OfficeID = Office.OFFICEID)
      ) AS TotalEmployees
FROM Office
LEFT JOIN (
   SELECT Attendance.AttDate
      , CASE attendanceStatus
         WHEN 377 THEN 1
         ELSE 0
         END AS Present
      , CASE attendanceStatus
         WHEN 378 THEN 1
         ELSE 0
        END AS Leave
      , EMP_1.OfficeID
   FROM EMP AS EMP_1
   INNER JOIN Attendance ON EMP_1.NewEmpID = Attendance.EmpID
   WHERE (Attendance.AttDate = '10-01-2010')
   ) AS AttDet ON Office.OFFICEID = AttDet.OfficeID
   AND Office.ISACTIVE = 1
GROUP BY AttDet.AttDate
   , Office.OFFICENAME
   , Office.OFFICEID
   , Office.ISACTIVE
HAVING (Office.ISACTIVE = 1)

,输出为:

officeid  officename        present  leave  absent    total
2         16-S, Office      209      5      77        291
3         41-N, Office      95       3      25        123
4         Abbotabad 3       0        0      3

但我想添加一个列restdays并检查有多少员工在休息日,我有一个表restday,列是empid restday

0 个答案:

没有答案