使用SQL计算每天多个作业的加班时间

时间:2015-03-25 13:57:16

标签: mysql sql

使用此示例https://rickosborne.org/blog/2006/01/calculating-overtime-with-pure-sql/,我有以下查询:

SELECT
    Employee,
    In_Day,
    Job_Code2,
    Job_Code,
    CASE
        WHEN (HoursBefore < 40.0)
        AND (HoursAfter > 40.0)
        THEN
            /* straddler */
            CASE
                WHEN p.i = 0
                THEN Hours + (40.0 - HoursAfter)
                ELSE HoursAfter - 40.0
            END
            /* hours adjustment */
        ELSE Hours
            /* no adjustment */
    END AS Hours,
    CASE
        WHEN (HoursBefore < 40.0)
        AND (HoursAfter > 40.0)
        THEN
            CASE
                WHEN p.i = 0
                THEN 1.0
                ELSE 1.5
            END
            /* straddlers */
        WHEN HoursBefore >= 40.0
        THEN 1.5
            /* non-straddlers after 40 hours */
        ELSE 1
            /* non-straddlers before 40 hours */
    END AS Rate,
    HoursBefore,
    HoursAfter
FROM
    (
        SELECT
            a.Employee,
            a.In_Day,
            a.Job_Code2,
            a.Job_Code,
            COALESCE(SUM(b.Hours),0) AS HoursBefore,
            a.Hours,
            COALESCE(SUM(b.Hours),0) + a.Hours AS HoursAfter
        FROM
            v_TimeSheet a
        LEFT OUTER JOIN
            v_TimeSheet b
        ON
            (
                a.Employee = b.Employee)
        AND (
                b.In_Day < a.In_Day)

        GROUP BY
            a.Employee,
            a.In_Day,
            a.Job_Code,
            a.Hours ) AS c

INNER JOIN
    Pivot p
ON
    (
        i <
        CASE
            WHEN (HoursBefore < 40.0)
            AND (HoursAfter > 40.0)
            THEN 2
            ELSE 1
        END)
ORDER BY
    Employee,
    In_Day,
    Job_Code; 

我在计算每天的小时数时遇到过一个问题。结果适用于桌面上90%的员工,但它似乎并没有逐个加起来。

这是它当前所做的一个示例输出(为我的原因稍作修改):

Employee    In_Day  Job_Code2   Job_Code    Hours   Rate    HoursBefore HoursAfter
EMPLOYEE 1  3/16/2015   13041   90100       5       1       0           5
EMPLOYEE 1  3/16/2015   13041   90400       5       1       0           5
EMPLOYEE 1  3/17/2015   13041   90100       4       1       10          14
EMPLOYEE 1  3/17/2015   13041   90400       2       1       10          12
EMPLOYEE 1  3/17/2015   13041   90500       2.5     1       10          12.5
EMPLOYEE 1  3/17/2015   13041   300100      2.5     1       10          12.5
EMPLOYEE 1  3/18/2015   13041   90100       4       1       21          25
EMPLOYEE 1  3/18/2015   13041   90400       3       1       21          24
EMPLOYEE 1  3/18/2015   13041   90500       4       1       21          25
EMPLOYEE 1  3/19/2015   13041   90100       2       1       32          34
EMPLOYEE 1  3/19/2015   13041   90400       2       1       32          34
EMPLOYEE 1  3/19/2015   13041   90500       4       1       32          36
EMPLOYEE 1  3/19/2015   13041   300100      3       1       32          35
EMPLOYEE 1  3/20/2015   13041   90100       2       1.5     43          45
EMPLOYEE 1  3/20/2015   13041   90400       4       1.5     43          47
EMPLOYEE 1  3/20/2015   13041   90500       4       1.5     43          47
EMPLOYEE 1  3/21/2015   13041   90100       3       1.5     53          56
EMPLOYEE 1  3/21/2015   13041   90400       3       1.5     53          56
EMPLOYEE 1  3/21/2015   13041   90500       2       1.5     53          55
EMPLOYEE 1  3/21/2015   13041   300100      2       1.5     53          55
EMPLOYEE 2  3/16/2015   14071   100A19      4.5     1       0           4.5
EMPLOYEE 2  3/16/2015   14071   100A23      5       1       0           5
EMPLOYEE 2  3/17/2015   14071   100A19      2.5     1       9.5         12
EMPLOYEE 2  3/17/2015   14071   100A23      7       1       9.5         16.5
EMPLOYEE 2  3/18/2015   14071   100A19      3       1       19          22
EMPLOYEE 2  3/18/2015   14071   100A24      5.5     1       19          24.5
EMPLOYEE 2  3/20/2015   14071   100A24      9.5     1       27.5        37
EMPLOYEE 2  3/21/2015   14071   100A24      3       1       37          46.5
EMPLOYEE 2  3/21/2015   14071   100A24      6.5     1.5     37          46.5

对于EMPLOYEE 2,通过将3/21记录分成2并且仅有6.5小时的加班时间,可以正确计算所有内容。然而,在员工1上,由于3/19 Job_Code 300100被计算为常规时间而不是加班,他的加班时间为3小时。

我尝试了许多不同的分组方法,看看能否得到我需要的结果。

有没有人建议纠正这个问题,或者有其他解决办法?

0 个答案:

没有答案