【发布时间】:2018-07-07 11:42:21
【问题描述】:
我的 SQL Server 表中有列数据:
Swipe_Date_Time, Emp_num , Emp_name, Direction
方向代表“进入”或“退出”。
样本数据:
预期输出:在办公室内刷入的总天数(以分钟为单位)
【问题讨论】:
标签: sql sql-server database select
我的 SQL Server 表中有列数据:
Swipe_Date_Time, Emp_num , Emp_name, Direction
方向代表“进入”或“退出”。
样本数据:
预期输出:在办公室内刷入的总天数(以分钟为单位)
【问题讨论】:
标签: sql sql-server database select
以下查询可以为您提供所需的输出。
SELECT t1.Emp_num, t1.Emp_name, t1.Swap_Date_Time AS Entry_Swipe_Time, ctbl.Exit_Swipe_Time, DATEDIFF(minute,t1.Swap_Date_Time, ctbl.Exit_Swipe_Time) AS ConsumedMinutes
FROM Sampletable t1 WITH(NOLOCK)
CROSS APPLY (
SELECT t2.Swap_Date_Time AS Exit_Swipe_Time
FROM Sampletable t2 WITH(NOLOCK)
WHERE t2.direction = 'exit' and t2.Emp_num = t1.Emp_num
) AS ctbl
WHERE t1.direction = 'entry'
【讨论】:
这可以通过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
【讨论】:
条件: 在包含“Exit”的行之前必须有一行“Entry”
✓Create table Tbl01 ( Swipe_Date_Time datetime, Emp_num Int, Emp_name NVarchar(10), Direction NVarchar(10) ) GO
Swipe_Date_Time | Emp_num | Emp_name |方向 :----------------- | ------: | :------- | :-------- 2018 年 1 月 6 日 10:34:00 | 1111 | XYZ |入口 2018 年 1 月 6 日 10:35:00 | 1111 | XYZ |出口 2018 年 1 月 6 日 10:37:00 | 1111 | XYZ |入口 2018 年 1 月 6 日 13:23:00 | 1111 | XYZ |出口 2018 年 1 月 6 日 18:39:00 | 1111 | XYZ |入口 2018 年 1 月 6 日 21:42:00 | 1111 | XYZ |入口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_Exit | Emp_num :-------------------- | ------: 2018 年 1 月 6 日 10:35:00 | 1111 2018 年 1 月 6 日 13:23:00 | 1111SELECT Swipe_Date_Time AS Swipe_Date_Time_Exit, Emp_num INTO #T01 FROM Tbl01 WHERE Direction = 'Exit' SELECT * FROM #T01 GO
Emp_num | Swipe_Date_Time_Exit | Swipe_Date_Time_Entry ------: | :-------------------- | :-------------------- 1111 | 2018 年 1 月 6 日 10:35:00 | 2018 年 1 月 6 日 10:34:00 1111 | 2018 年 1 月 6 日 13:23:00 | 2018 年 1 月 6 日 10:37:00SELECT 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
Swipe_Date_Time_Entry_As_Day | Swipe_Date_Time_Diff :---------------------------- | ------------------: 2018 年 1 月 6 日 00:00:00 | 167SELECT 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 :---------------------------- | ------------------: 2018 年 1 月 6 日 00:00:00 | 167SELECT 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
db小提琴here
【讨论】: