SQL Server数据透视表出席记录根据超时和超时

时间:2018-09-25 05:59:42

标签: sql-server tsql pivot

有人知道如何提出我的要求的解决方案吗? 我的出勤表结构([ID],[DATE],[EMPID],[TIME])。见下文:

ID  DATE        EMPID   TIME        REMARKS
1   20/09/2018  9001    7:30        This will be the In_1 as it is the first TIME-IN in for EMPID=9001
2   20/09/2018  9001    7:40        This will be the In_2 as it is the second TIME-IN for EMPID=9001
3   20/09/2018  9001    7:50        Not included
4   20/09/2018  9001    17:10       This will be the Out_1 as it is the first TIME-OUT for EMPID=9001
5   20/09/2018  9001    17:50       This will be the Out_2 as it is the second TIME-OUT for EMPID=9001
6   20/09/2018  9001    18:00       Not included
7   20/09/2018  9002    7:20        This will be the In_1 as it is the first TIME-IN in for EMPID=9002
8   20/09/2018  9002    7:21        This will be the In_2 as it is the second TIME-IN for EMPID=9002
9   20/09/2018  9002    18:00       This will be the Out_1 as it is the first TIME-OUT for EMPID=9002
10  20/09/2018  9003    7:00        This will be the In_1 as it is the first TIME-IN in for EMPID=9003
11  20/09/2018  9003    17:10       This will be the Out_1 as it is the first TIME-OUT for EMPID=9003
11  20/09/2018  9003    17:12       This will be the Out_2 as it is the second TIME-OUT for EMPID=9003
11  20/09/2018  9003    17:15       Not included

输出将与以下相同:

DATE        EMPID   IN_1    IN_2    OUT_1   OUT_2
20/09/2018  9001    7:30    7:40    17:10   17:50
20/09/2018  9002    7:20    7:21    18:00   
20/09/2018  9003    7:20    17:10   17:12

2 个答案:

答案 0 :(得分:1)

由于您没有可以从OUT识别IN的列,因此我可以任意使用12:00作为分隔时间。

; WITH
cte AS
(
    SELECT  *, TYPE = CASE WHEN TIME < '12:00' THEN 'IN' ELSE 'OUT' END
    FROM    your_table
),
cte2 AS
(
    SELECT  *, rn = row_number() over (partition by EMPID, DATE, TYPE ORDER BY TIME)
    FROM    cte
)
SELECT DATE, EMPID,
       IN_1  = MAX(CASE WHEN TYPE = 'IN'  AND rn = 1 THEN TIME END),
       IN_2  = MAX(CASE WHEN TYPE = 'IN'  AND rn = 2 THEN TIME END),
       OUT_1 = MAX(CASE WHEN TYPE = 'OUT' AND rn = 1 THEN TIME END),
       OUT_2 = MAX(CASE WHEN TYPE = 'OUT' AND rn = 2 THEN TIME END)
FROM    cte2
GROUP BY DATE, EMPID

答案 1 :(得分:0)

试试这个枢轴

;WITH CTE(ID,[DATE], EMPID,[TIME], REMARKS)
AS
(
SELECT 1 ,'20/09/2018', 9001,'7:30' ,'This will be the In_1 as it is the first TIME-IN in for EMPID=9001'  UNION ALL
SELECT 2 ,'20/09/2018', 9001,'7:40' ,'This will be the In_2 as it is the second TIME-IN for EMPID=9001'    UNION ALL
SELECT 3 ,'20/09/2018', 9001,'7:50' ,' Not included'                                                       UNION ALL
SELECT 4 ,'20/09/2018', 9001,'17:10',' This will be the Out_1 as it is the first TIME-OUT for EMPID=9001'  UNION ALL
SELECT 5 ,'20/09/2018', 9001,'17:50',' This will be the Out_2 as it is the second TIME-OUT for EMPID=9001' UNION ALL
SELECT 6 ,'20/09/2018', 9001,'18:00',' Not included'                                                       UNION ALL
SELECT 7 ,'20/09/2018', 9002,'7:20' ,' This will be the In_1 as it is the first TIME-IN in for EMPID=9002' UNION ALL
SELECT 8 ,'20/09/2018', 9002,'7:21' ,' This will be the In_2 as it is the second TIME-IN for EMPID=9002'   UNION ALL
SELECT 9 ,'20/09/2018', 9002,'18:00',' This will be the Out_1 as it is the first TIME-OUT for EMPID=9002'  UNION ALL
SELECT 10,'20/09/2018', 9003,'7:00' ,' This will be the In_1 as it is the first TIME-IN in for EMPID=9003' UNION ALL
SELECT 11,'20/09/2018', 9003,'17:10',' This will be the Out_1 as it is the first TIME-OUT for EMPID=9003'  UNION ALL
SELECT 11,'20/09/2018', 9003,'17:12',' This will be the Out_2 as it is the second TIME-OUT for EMPID=9003' UNION ALL
SELECT 11,'20/09/2018', 9003,'17:15',' Not included'
)
SELECT [DATE], EMPID,[In_1],[In_2],[Out_1],[Out_2] FROM
(
SELECT [DATE], EMPID,[TIME]
,MAX(CASE  WHEN  CHARINDEX('In_1',REMARKS)>0 THEN 'In_1' 
           WHEN  CHARINDEX('In_2',REMARKS)>0 THEN 'In_2'
           WHEN  CHARINDEX('Out_1',REMARKS)>0 THEN 'Out_1'
           WHEN  CHARINDEX('Out_2',REMARKS)>0 THEN 'Out_2'
    ELSE 'NA' END)  AS Details
FROM CTE
GROUP BY [DATE], EMPID,[TIME]
)dt
PIVOT
(
MAX([TIME]) FOR Details IN ([In_1],[In_2],[Out_1],[Out_2])
)pvt

结果

DATE        EMPID   In_1    In_2    Out_1   Out_2
-------------------------------------------------
20/09/2018  9001    7:30    7:40    17:10   17:50
20/09/2018  9002    7:20    7:21    18:00   NULL
20/09/2018  9003    7:00    NULL    17:10   17:12