Datediff找到休息时间的差异

时间:2013-12-19 04:02:51

标签: sql sql-server vb.net sql-server-2005

我有一个查询,拉动运行一个功能,以抓住员工信息。它会拉出时间段,将它们组合在一起,然后运行检查以查看

1)那天没有休息的人 2)谁休息不到30分钟

这部分的第一部分正常工作。它显示了一周内没有休息的7个人(所有人都休息了0分钟)。但是,它不会显示仅持续22分钟休息的用户。我相信正在发生的事情,是它没有完全结合特定日期的所有时间信息。它只是为那些没有休息的人提供信息。有什么帮助吗?

我的查询是:

    SELECT        lEmployeeID, NUM_Hours, CASE WHEN tmp.Break_Time_Minutes < 0 THEN 0 ELSE Break_Time_Minutes END AS Break_Time_Minutes, 
                         CASE WHEN tmp.Break_Time_Minutes < 0 THEN 1 ELSE 0 END AS SkippedBreak, sFirstName, sLastName, TotalHours
FROM            (SELECT        employee.lEmployeeID, employee.sFirstName, employee.sLastName, employee.TotalHours, ROUND(SUM(DATEDIFF(second, employee.dtTimeIn, 
                                                    employee.dtTimeOut) / 60.0 / 60.0), 1) AS NUM_Hours, DATEDIFF(mi, MIN(employee.dtTimeOut), MAX(employee.dtTimeIn)) AS Break_Time_Minutes
                          FROM            dbo.fTimeCard(@StartDate, @EndDate, @DeptList, @iActive, @EmployeeList) AS employee LEFT OUTER JOIN
                                                    Employees AS e ON employee.lEmployeeID = e.lEmployeeID
                          WHERE        (employee.TotalHours >= 0) AND (employee.DID IS NOT NULL) OR
                                                    (employee.DID IS NOT NULL) AND (employee.dtTimeOut IS NULL)
                          GROUP BY employee.lEmployeeID, employee.sLastName, employee.sFirstName, employee.TotalHours, CAST(employee.dtTimeIn AS Datetime)) AS tmp
WHERE        (NUM_Hours > 6) AND (Break_Time_Minutes < 30)

1 个答案:

答案 0 :(得分:0)

格式化查询有助于我理解它。

问题似乎在于计算休息时间。这些计算如何准确?

         ROUND(SUM(DATEDIFF(second, employee.dtTimeIn, 
                            employee.dtTimeOut) / 60.0 / 60.0), 1) AS NUM_Hours,
         DATEDIFF(mi, MIN(employee.dtTimeIn), MAX(employee.dtTimeOut)
                 ) AS Break_Time_Minutes

第二个是将休息时间计算为时间的最小值和超时的最大值。据推测,这是员工在那里的总时间。

也许你的意思是:

      MIN(DATEDIFF(mi, employee.dtTimeIn, employee.dtTimeOut)
         ) AS Break_Time_Minutes

完整格式化查询是:

SELECT lEmployeeID, NUM_Hours,
       (CASE WHEN tmp.Break_Time_Minutes < 0 THEN 0 ELSE Break_Time_Minutes
        END) AS Break_Time_Minutes, 
       (CASE WHEN tmp.Break_Time_Minutes < 0 THEN 1 ELSE 0
        END) AS SkippedBreak,
       sFirstName, sLastName, TotalHours
FROM (SELECT employee.lEmployeeID, employee.sFirstName, employee.sLastName, 
             employee.TotalHours,
             ROUND(SUM(DATEDIFF(second, employee.dtTimeIn, 
                                employee.dtTimeOut) / 60.0 / 60.0), 1) AS NUM_Hours,
             DATEDIFF(mi, MIN(employee.dtTimeIn), MAX(employee.dtTimeOut)
                     ) AS Break_Time_Minutes
      FROM dbo.fTimeCard(@StartDate, @EndDate, @DeptList, @iActive, @EmployeeList
                        ) employee LEFT OUTER JOIN
           Employees e
           ON employee.lEmployeeID = e.lEmployeeID
      WHERE (employee.TotalHours >= 0) AND (employee.DID IS NOT NULL) OR
            (employee.DID IS NOT NULL) AND (employee.dtTimeOut IS NULL)
      GROUP BY employee.lEmployeeID, employee.sLastName, employee.sFirstName,
               employee.TotalHours, CAST(employee.dtTimeIn AS Datetime)
     ) tmp
WHERE (NUM_Hours > 6) AND (Break_Time_Minutes < 30)