用于计算工时的SQL查询

时间:2017-02-10 14:29:13

标签: sql sql-server sql-server-2008

我创建了一个查询,在时钟输入/输出系统中收集人们的时间,并将它们命令到正确的一天,因为这些是近移,所以跨越两天,这里是查询和输出

SELECT 
    china_vision_pubpersonnel.NAME, 
    china_vision_pubpersonnel.id, 
    china_vision_pubcards.cardcode, 
    china_vision_pubpersonnel.telephone, 
    atdshiftdetail.endoverday, 
    china_vision_dorevents.eventtm AS T, 
    CONVERT(VARCHAR(10), china_vision_dorevents.eventtm, 103) AS Day, 
    Floor(CONVERT(FLOAT, Dateadd(hour, 4, eventtm))) AS DayNumber,
    Row_number() OVER(PARTITION BY Floor(CONVERT(FLOAT, Floor(CONVERT(FLOAT, Dateadd(hour, 6, eventtm))))) 
                      ORDER BY Floor(CONVERT(FLOAT, Dateadd(hour, 5, eventtm)))) InDay 
FROM   
    china_vision_pubcards 
INNER JOIN 
    china_vision_pubpersonnel ON china_vision_pubcards.pubpersonnel_ref = china_vision_pubpersonnel.reference 
INNER JOIN 
    china_vision_dorevents ON china_vision_pubcards.cardcode = china_vision_dorevents.cardcode 
INNER JOIN 
    atdshiftdetail ON RIGHT(china_vision_pubpersonnel.id, 4) = atdshiftdetail.NAME 
WHERE  
    (china_vision_dorevents.dorctrls_ref = '16') 
    AND (CONVERT(DATE, china_vision_dorevents.eventtm) > Dateadd(day, -730, Getdate())) 
    AND atdshiftdetail.endoverday = '1' 

此查询的结果大多数列名都是自解释的但是

T是输入时间

日是输入日

日期编号是输入数字的天数列表,因为您可以每天使用相同的数字

InDay是输入的顺序,所以你可以看到1总是在晚上2和3正在从刹车回来,4正在离开

    Name  | ID  | CardCode | Telephone | EndOverDay  |           T (Time)        |   Day        |  DayNumber  | InDay
    ------------------------------------------------------------------------------------------------------------------
    Name  | ID  | CardCode |    9      |    1        |   2017-02-10 07:52:00.000 | 10/02/2017   |    42774    |  4
    Name  | ID  | CardCode |    9      |    1        |   2017-02-10 03:28:31.000 | 10/02/2017   |    42774    |  3
    Name  | ID  | CardCode |    9      |    1        |   2017-02-10 02:57:14.000 | 10/02/2017   |    42774    |  2
    Name  | ID  | CardCode |    9      |    1        |   2017-02-09 19:18:19.000 | 09/02/2017   |    42773    |  1
    Name  | ID  | CardCode |    9      |    1        |   2017-02-09 07:50:55.000 | 09/02/2017   |    42773    |  4
    Name  | ID  | CardCode |    9      |    1        |   2017-02-09 03:28:41.000 | 09/02/2017   |    42773    |  3
    Name  | ID  | CardCode |    9      |    1        |   2017-02-09 02:56:14.000 | 09/02/2017   |    42773    |  2
    Name  | ID  | CardCode |    9      |    1        |   2017-02-08 19:19:05.000 | 08/02/2017   |    42772    |  1
    Name  | ID  | CardCode |    9      |    1        |   2017-02-08 07:43:06.000 | 08/02/2017   |    42772    |  4
    Name  | ID  | CardCode |    9      |    1        |   2017-02-08 03:26:33.000 | 08/02/2017   |    42772    |  3
    Name  | ID  | CardCode |    9      |    1        |   2017-02-08 02:54:58.000 | 08/02/2017   |    42772    |  2
    Name  | ID  | CardCode |    9      |    1        |   2017-02-07 19:15:46.000 | 07/02/2017   |    42771    |  1

所以最终目标应该是将Indays 1-2加在一起,3-4组合在一起,然后将这两个结果加到那一天

这一天是Inday 1登陆的日子

1 个答案:

答案 0 :(得分:0)

以下是您的数据和临时表的示例。这应该让你开始。显然需要为您的结果做一些工作。

    CREATE TABLE #TempTable ( 
                            EmpName         VARCHAR(10)
                            , ID            INT
                            , CardCode      VARCHAR(10)
                            , Telephone     VARCHAR(10)
                            , EndOverDay    int
                            , T             DATETIME
                            , TDay          DATETIME
                            , DayNumber     BIGINT
                            , InDay         INT
                        )

INSERT INTO #TempTable
VALUES ('Rick Flair',   1,  'CardCode', 9,  1,  '2017-02-10 07:52:00.000',  '10/02/2017',   42774,  4)
INSERT INTO #TempTable
VALUES ('Rick Flair', 1,    'CardCode', 9,  1,  '2017-02-10 03:28:31.000',  '10/02/2017',   42774,  3)
INSERT INTO #TempTable
VALUES ('Rick Flair',   1,  'CardCode', 9,  1,  '2017-02-10 02:57:14.000',  '10/02/2017',   42774,  2)
INSERT INTO #TempTable
VALUES ('Rick Flair',   1,  'CardCode', 9,  1,  '2017-02-09 19:18:19.000',  '09/02/2017',   42773,  1)
INSERT INTO #TempTable
VALUES ('Scott Hall',   2,  'CardCode', 9,  1,  '2017-02-09 07:50:55.000',  '09/02/2017',   42773,  4)
INSERT INTO #TempTable
VALUES ('Scott Hall',   2,  'CardCode', 9,  1,  '2017-02-09 03:28:41.000',  '09/02/2017',   42773,  3)
INSERT INTO #TempTable
VALUES ('Scott Hall',   2,  'CardCode', 9,  1,  '2017-02-09 02:56:14.000',  '09/02/2017',   42773,  2)
INSERT INTO #TempTable
VALUES ('Scott Hall',   2,  'CardCode', 9,  1,  '2017-02-08 19:19:05.000',  '08/02/2017',   42772,  1)
INSERT INTO #TempTable
VALUES ('Kevin Nash',   3,  'CardCode', 9,  1,  '2017-02-08 07:43:06.000',  '08/02/2017',   42772,  4)
INSERT INTO #TempTable
VALUES ('Kevin Nash',   3,  'CardCode', 9,  1,  '2017-02-08 03:26:33.000',  '08/02/2017',   42772,  3)
INSERT INTO #TempTable
VALUES ('Kevin Nash',   3,  'CardCode', 9,  1,  '2017-02-08 02:54:58.000',  '08/02/2017',   42772,  2)
INSERT INTO #TempTable
VALUES ('Kevin Nash',   3,  'CardCode', 9,  1,  '2017-02-07 19:15:46.000',  '07/02/2017',   42771,  1)


SELECT A.EmpName, SUM(A.HoursWorked) AS HoursWorked
FROM (
        SELECT t1.EmpName, t1.id, t1.t AS Time1, t2.t AS Time2, datediff(HOUR, t1.t, t2.t) AS HoursWorked
        FROM #TempTable t1
            JOIN (
                    SELECT EmpName ,
                           ID ,
                           CardCode ,
                           Telephone ,
                           EndOverDay ,
                           T ,
                           TDay ,
                           DayNumber ,
                           InDay
                    FROM #TempTable 
                    WHERE InDay = 2
                ) t2 ON
                t1.id = t2.id
        WHERE t1.InDay = 1

        UNION all

        SELECT t1.EmpName, t1.id, t1.t AS Time1, t2.t AS Time2, datediff(HOUR, t1.t, t2.t) AS HoursWorked
        FROM #TempTable t1
            JOIN (
                    SELECT EmpName ,
                           ID ,
                           CardCode ,
                           Telephone ,
                           EndOverDay ,
                           T ,
                           TDay ,
                           DayNumber ,
                           InDay
                    FROM #TempTable 
                    WHERE InDay = 4
                ) t2 ON
                t1.id = t2.id
        WHERE t1.InDay = 3
    ) A
GROUP BY A.EmpName;


DROP TABLE #TempTable