SQL数据提取和日期差异

时间:2018-07-07 11:42:22

标签: sql sql-server database select

我在SQL Server表中具有列的数据:

Swipe_Date_Time, Emp_num , Emp_name, Direction

方向代表“进入”或“退出”。

样本数据:

enter image description here

预期输出:在办公室内以分钟为单位的全天刷卡

3 个答案:

答案 0 :(得分:0)

条件: 包含“退出”的行之前必须有“进入”行

Create table Tbl01 (
   Swipe_Date_Time datetime,
   Emp_num Int, 
   Emp_name NVarchar(10), 
   Direction NVarchar(10)
)
GO
INSERT INTO Tbl01 (Swipe_Date_Time,Emp_num,Emp_name,Direction) VALUES ('2018-06-01 10:34',1111,'XYZ','Entry')
INSERT INTO Tbl01 (Swipe_Date_Time,Emp_num,Emp_name,Direction) VALUES ('2018-06-01 10:35',1111,'XYZ','Exit')
INSERT INTO Tbl01 (Swipe_Date_Time,Emp_num,Emp_name,Direction) VALUES ('2018-06-01 10:37',1111,'XYZ','Entry')
INSERT INTO Tbl01 (Swipe_Date_Time,Emp_num,Emp_name,Direction) VALUES ('2018-06-01 13:23',1111,'XYZ','Exit')
INSERT INTO Tbl01 (Swipe_Date_Time,Emp_num,Emp_name,Direction) VALUES ('2018-06-01 18:39',1111,'XYZ','Entry')
INSERT INTO Tbl01 (Swipe_Date_Time,Emp_num,Emp_name,Direction) VALUES ('2018-06-01 21:42',1111,'XYZ','Entry')

select * FROM Tbl01
GO
Swipe_Date_Time     | Emp_num | Emp_name | Direction
:------------------ | ------: | :------- | :--------
01/06/2018 10:34:00 |    1111 | XYZ      | Entry    
01/06/2018 10:35:00 |    1111 | XYZ      | Exit     
01/06/2018 10:37:00 |    1111 | XYZ      | Entry    
01/06/2018 13:23:00 |    1111 | XYZ      | Exit     
01/06/2018 18:39:00 |    1111 | XYZ      | Entry    
01/06/2018 21:42:00 |    1111 | XYZ      | Entry    
SELECT
  Swipe_Date_Time AS Swipe_Date_Time_Exit,
  Emp_num 
INTO #T01
FROM Tbl01
WHERE
  Direction = 'Exit'

SELECT * FROM #T01
GO
Swipe_Date_Time_Exit | Emp_num
:------------------- | ------:
01/06/2018 10:35:00  |    1111
01/06/2018 13:23:00  |    1111
SELECT
  Tbl01.Emp_num,
  #T01.Swipe_Date_Time_Exit,
  MAX(Tbl01.Swipe_Date_Time) AS Swipe_Date_Time_Entry
INTO #T02
FROM Tbl01 
  JOIN #T01 ON Tbl01.Emp_num = #T01.Emp_num
WHERE
  Tbl01.Direction = 'Entry'
  AND Tbl01.Swipe_Date_Time < #T01.Swipe_Date_Time_Exit
GROUP BY 
  Tbl01.Emp_num,
  #T01.Swipe_Date_Time_Exit

SELECT * FROM #T02
GO
Emp_num | Swipe_Date_Time_Exit | Swipe_Date_Time_Entry
------: | :------------------- | :--------------------
   1111 | 01/06/2018 10:35:00  | 01/06/2018 10:34:00  
   1111 | 01/06/2018 13:23:00  | 01/06/2018 10:37:00  
  SELECT 
   CAST(#T02.Swipe_Date_Time_Entry AS DATE) AS Swipe_Date_Time_Entry_As_Day,
   SUM( DateDiff(minute,#T02.Swipe_Date_Time_Entry,#T02.Swipe_Date_Time_Exit)) AS Swipe_Date_Time_Diff
  FROM #T02
  GROUP BY 
    CAST(#T02.Swipe_Date_Time_Entry AS DATE)
GO
Swipe_Date_Time_Entry_As_Day | Swipe_Date_Time_Diff
:--------------------------- | -------------------:
01/06/2018 00:00:00          |                  167

结果查询

SELECT 
   CAST(T02.Swipe_Date_Time_Entry AS DATE) AS Swipe_Date_Time_Entry_As_Day,
   SUM( DateDiff(minute,T02.Swipe_Date_Time_Entry,T02.Swipe_Date_Time_Exit)) AS Swipe_Date_Time_Diff
FROM 
  (
   SELECT
      Tbl01.Emp_num,
      T01.Swipe_Date_Time_Exit,
      MAX(Tbl01.Swipe_Date_Time) AS Swipe_Date_Time_Entry
   FROM Tbl01 
      JOIN 
      (
        SELECT
           Swipe_Date_Time AS Swipe_Date_Time_Exit,
           Emp_num 
        FROM Tbl01
        WHERE
           Direction = 'Exit'
      )
      AS T01 ON Tbl01.Emp_num = T01.Emp_num
   WHERE
      Tbl01.Direction = 'Entry'
      AND Tbl01.Swipe_Date_Time < T01.Swipe_Date_Time_Exit
   GROUP BY 
      Tbl01.Emp_num,
      T01.Swipe_Date_Time_Exit
  )
  AS T02
GROUP BY 
   CAST(T02.Swipe_Date_Time_Entry AS DATE)
GO
Swipe_Date_Time_Entry_As_Day | Swipe_Date_Time_Diff
:--------------------------- | -------------------:
01/06/2018 00:00:00          |                  167

db <>提琴here

答案 1 :(得分:0)

以下查询可以为您提供所需的输出。

POST https://mydomain.auth.us-east-1.amazoncognito.com/oauth2/token& Content-Type='application/x-www-form-urlencoded'& Authorization=Basic aSdxd892iujendek328uedj grant_type=authorization_code& client_id=djc98u3jiedmi283eu928& code=AUTHORIZATION_CODE& redirect_uri=com.myclientapp://myclient/redirect

答案 2 :(得分:0)

这可以通过LEAD()函数来完成,以获取每个Emp_num的下一个日期时间。然后,您只需要获取这些分钟的总和,并按日期分别对每个Emp_num进行分组。这样一来,您便可以在几分钟之内获得每天每位员工在办公室内的总刷卡次数。

查询:

 SELECT 
    SwipeDate
,   Emp_num
,   Emp_name
,   SUM(TotalSwipeMinutes) AS TotalSwipeMinutes
FROM(
SELECT 
    Swipe_Date_Time
,   Direction
,   LEAD(Swipe_Date_Time) OVER(PARTITION BY Emp_num ORDER BY Swipe_Date_Time) AS NextSwipe
,   LEAD(Direction)  OVER(PARTITION BY Emp_num ORDER BY Swipe_Date_Time) AS NextDirection
,   Emp_num
,   Emp_name
,   DATEDIFF(MINUTE, Swipe_Date_Time, LEAD(Swipe_Date_Time) OVER(PARTITION BY Emp_num ORDER BY Swipe_Date_Time) ) AS TotalSwipeMinutes
,   CAST(Swipe_Date_Time AS DATE) AS SwipeDate
FROM OfficeSwipe
) D
GROUP BY 
    SwipeDate
,   Emp_num
,   Emp_name

结果:

|  SwipeDate | Emp_num | Emp_name | TotalSwipeMinutes |
|------------|---------|----------|-------------------|
| 2018-06-01 |    1111 |      XYZ |               668 |

演示:SQLFiddle