在同一行中选择顶行数据(如果同一列中的数据差异小于 'n' 小时)sql
Selecting top row data in the same row (if the data difference is less than 'n' hours within the same column) sql
我有这个现有数据需要排序,我的问题是我们如何对同一列内的数据进行排序,如果值之间的差异小于3小时,将选择最大值。
这合理吗?
根据下面的table,2019-12-25有3个值,
| 2019-12-25 14:00:02.000 | 2019-12-25 |
| 2019-12-25 15:39:57.000 | 2019-12-25 |
| 2019-12-25 22:39:57.000 | 2019-12-25 |
我想从列表中删除 2019-12-25 14:00:02.000 以便保留的数据将是 2019- 12-25 15:39:57.000 和 2019-12-25 22:39:57.000.
我要实现的流程是,如果时间戳之间的日期时差在 3 小时以内,则只会选择最大值,在这种情况下为 2019-12-25 15:39:57。将选择 000,因为 2019-12-25 14:00:02.000 和 2019-12-25 15:39:57.000 在相同的 3 小时范围内,2019-12-25 22:39:57。 000 将保留在 table 中,因为它与 2 个值相比超过 3 小时。
有没有办法让它起作用?
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| badgenumber | checktype | recordout | checkdate | employeeidno | fullname | departmentname |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-21 23:43:36.000 | 2019-12-21 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-22 22:36:50.000 | 2019-12-22 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-23 18:03:16.000 | 2019-12-23 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-24 22:06:58.000 | 2019-12-24 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-25 14:00:02.000 | 2019-12-25 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-25 15:39:57.000 | 2019-12-25 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-25 22:39:57.000 | 2019-12-25 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-26 14:00:02.000 | 2019-12-26 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-28 22:00:01.000 | 2019-12-28 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-28 23:31:11.000 | 2019-12-28 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-29 15:08:10.000 | 2019-12-29 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-30 16:03:20.000 | 2019-12-30 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2020-01-02 06:52:18.000 | 2020-01-02 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2020-01-03 08:00:57.000 | 2020-01-03 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2020-01-04 06:40:11.000 | 2020-01-04 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
示例。
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| badgenumber | checktype | recordout | checkdate | employeeidno | fullname | departmentname |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-25 14:00:02.000 | 2019-12-25 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-25 15:39:57.000 | 2019-12-25 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-25 22:39:57.000 | 2019-12-25 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
2个时间戳相差不到3小时。因此 3:39:57 PM 将被选中,而 10:39:57 PM 将被忽略并保留在 table 因为它与同一列中的其他 2 个数据相比超过 3 小时。
这是我的预期输出。
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| badgenumber | checktype | recordout | checkdate | employeeidno | fullname | departmentname |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-21 23:43:36.000 | 2019-12-21 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-22 22:36:50.000 | 2019-12-22 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-23 18:03:16.000 | 2019-12-23 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-24 22:06:58.000 | 2019-12-24 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-25 14:00:02.000 | 2019-12-25 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-25 15:39:57.000 | 2019-12-25 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-25 22:39:57.000 | 2019-12-25 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-26 14:00:02.000 | 2019-12-26 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-28 22:00:01.000 | 2019-12-28 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-28 23:31:11.000 | 2019-12-28 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-29 15:08:10.000 | 2019-12-29 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-30 16:03:20.000 | 2019-12-30 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2020-01-02 06:52:18.000 | 2020-01-02 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2020-01-03 08:00:57.000 | 2020-01-03 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2020-01-04 06:40:11.000 | 2020-01-04 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
这是我需要它工作的代码部分。
(SELECT MAX(userinfo.badgenumber) AS badgenumber, MAX(RTRIM(checkinout.checktype)) AS 'checktype',
MAX(checkinout.checktime) as 'recordout', MAX(CONVERT(date,checkinout.checktime)) as checkdate,
MAX(RTRIM(employeemasterfile.employeeidno)) AS 'employeeidno', MAX(RTRIM(employeemasterfile.lastname))+', '+
MAX(RTRIM(employeemasterfile.firstname))+' '+MAX(LEFT(employeemasterfile.middlename,1))+'.' AS 'fullname',
MAX(RTRIM(departmentmasterfile.departmentname)) AS 'departmentname' FROM ((checkinout INNER JOIN userinfo
ON checkinout.userid = userinfo.userid) INNER JOIN employeemasterfile ON userinfo.badgenumber = employeemasterfile.fingerscanno)
INNER JOIN departmentmasterfile ON LEFT(employeemasterfile.employeeidno, 4) = LEFT(departmentmasterfile.departmentcode, 4)
WHERE CONVERT(date,checkinout.checktime) BETWEEN '2019-12-21' AND DATEADD(DAY, 1,'2020-01-05') AND fingerscanno = '1233'
AND CHECKINOUT.CHECKTYPE = 'O' COLLATE SQL_Latin1_General_CP1_CS_AS GROUP BY userinfo.badgenumber, LEFT(checkinout.checktime,14)) AS t2
ON
t2.recordout BETWEEN DATEADD(HOUR,-6,t0.mergetimeoutorig) AND DATEADD(HOUR, 6,t0.mergetimeoutorig)
Example value t0.mergetimeoutorig
= 25/12/2019
以下代码将为您提供需要排除的日期。
SELECT DS.*
FROM @DataSource DS1
CROSS APPLY
(
SELECT *
FROM @DataSource DS2
WHERE DS1.[checkdate] = DS2.[checkdate]
AND DATEDIFF(HOUR, DS1.[recordout], DS2.[recordout]) < 3
AND DS1.[recordout] < DS2.[recordout]
) DS;
然后你可以DELETE
从原来的table或SELECT
中只需要数据。这是完整的工作示例:
DECLARE @DataSource TABLE
(
[recordout] DATETIME
,[checkdate] DATE
);
INSERT INTO @DataSource ([recordout], [checkdate])
VALUES ('2019-12-21 23:43:36.000', '2019-12-21')
,('2019-12-22 22:36:50.000', '2019-12-22')
,('2019-12-23 18:03:16.000', '2019-12-23')
,('2019-12-24 22:06:58.000', '2019-12-24')
,('2019-12-25 01:39:57.000', '2019-12-25')
,('2019-12-25 02:39:57.000', '2019-12-25')
,('2019-12-25 02:49:57.000', '2019-12-25')
,('2019-12-25 14:00:02.000', '2019-12-25')
,('2019-12-25 15:39:57.000', '2019-12-25')
,('2019-12-25 22:39:57.000', '2019-12-25')
,('2019-12-26 14:00:02.000', '2019-12-26')
,('2019-12-28 22:00:01.000', '2019-12-28')
,('2019-12-28 23:31:11.000', '2019-12-28')
,('2019-12-29 15:08:10.000', '2019-12-29')
,('2019-12-30 16:03:20.000', '2019-12-30')
,('2020-01-02 06:52:18.000', '2020-01-02')
,('2020-01-03 08:00:57.000', '2020-01-03')
,('2020-01-04 06:40:11.000', '2020-01-04');
WITH DataSource AS
(
SELECT DS.*
FROM @DataSource DS1
CROSS APPLY
(
SELECT *
FROM @DataSource DS2
WHERE DS1.[checkdate] = DS2.[checkdate]
AND DATEDIFF(HOUR, DS1.[recordout], DS2.[recordout]) < 3
AND DS1.[recordout] < DS2.[recordout]
) DS
)
SELECT DS1.*
FROM @DataSource DS1
LEFT JOIN DataSource DS2
ON DS1.[checkdate] = DS2.[checkdate]
AND DS1.[recordout] = DS2.[recordout]
WHERE DS2.[checkdate] IS NULL
ORDER BY DS1.[recordout] ASC;
尝试在您的查询中实现以下逻辑
--Table where swipe details are stored
Create table #EmployeeSwipe
(
ID INT IDENTITY(1,1) primary key,
EmployeeID INT,
CheckType varchar(10),
SwipeTime DATETIME,
SwipeDate DATE
)
GO
--this table will have filtered row id's
CREATE Table #tempdata
(
RowID INT
)
GO
--Dump data
Insert into #EmployeeSwipe values(
1,'I','2019-12-27 10:43:36.000','12/27/2019'
)
Insert into #EmployeeSwipe values(
1,'O','2019-12-27 14:43:36.000','12/27/2019'
)
Insert into #EmployeeSwipe values(
1,'O','2019-12-27 15:43:36.000','12/27/2019'
)
Insert into #EmployeeSwipe values(
1,'O','2019-12-27 22:43:36.000','12/27/2019'
)
Insert into #EmployeeSwipe values(
2,'O','2019-12-27 14:43:36.000','12/27/2019'
)
Insert into #EmployeeSwipe values(
2,'O','2019-12-27 15:43:36.000','12/27/2019'
)
Insert into #EmployeeSwipe values(
3,'O','2019-12-27 14:43:36.000','12/27/2019'
)
GO
;with cteres
AS
(
SELECT
P.ID,P.EmployeeID ,P.SwipeDate ,
p.SwipeTime,
LEAD(p.SwipeTime) OVER(Partition By P.EmployeeID ORDER BY P.ID) NextValue
FROM #EmployeeSwipe P Where checktype='O'
)
--Here check if difference between two swap time is more than 3 hours
Insert into #tempdata
select ID from
(select CASE WHEN
DATEDIFF(HOUR,SwipeTime,NextValue) > 3 THEN 1
WHEN DATEDIFF(HOUR,SwipeTime,NextValue) IS NULL THEN 1
ELSE 0 END as IsValid,ID from cteres
) --where NextValue is not null)
mres where mres.IsValid = 1
--This will give you final output
select ES.* from #tempdata T JOIN #EmployeeSwipe ES ON T.RowID = ES.ID
我有这个现有数据需要排序,我的问题是我们如何对同一列内的数据进行排序,如果值之间的差异小于3小时,将选择最大值。
这合理吗?
根据下面的table,2019-12-25有3个值,
| 2019-12-25 14:00:02.000 | 2019-12-25 |
| 2019-12-25 15:39:57.000 | 2019-12-25 |
| 2019-12-25 22:39:57.000 | 2019-12-25 |
我想从列表中删除 2019-12-25 14:00:02.000 以便保留的数据将是 2019- 12-25 15:39:57.000 和 2019-12-25 22:39:57.000.
我要实现的流程是,如果时间戳之间的日期时差在 3 小时以内,则只会选择最大值,在这种情况下为 2019-12-25 15:39:57。将选择 000,因为 2019-12-25 14:00:02.000 和 2019-12-25 15:39:57.000 在相同的 3 小时范围内,2019-12-25 22:39:57。 000 将保留在 table 中,因为它与 2 个值相比超过 3 小时。
有没有办法让它起作用?
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| badgenumber | checktype | recordout | checkdate | employeeidno | fullname | departmentname |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-21 23:43:36.000 | 2019-12-21 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-22 22:36:50.000 | 2019-12-22 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-23 18:03:16.000 | 2019-12-23 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-24 22:06:58.000 | 2019-12-24 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-25 14:00:02.000 | 2019-12-25 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-25 15:39:57.000 | 2019-12-25 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-25 22:39:57.000 | 2019-12-25 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-26 14:00:02.000 | 2019-12-26 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-28 22:00:01.000 | 2019-12-28 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-28 23:31:11.000 | 2019-12-28 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-29 15:08:10.000 | 2019-12-29 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-30 16:03:20.000 | 2019-12-30 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2020-01-02 06:52:18.000 | 2020-01-02 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2020-01-03 08:00:57.000 | 2020-01-03 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2020-01-04 06:40:11.000 | 2020-01-04 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
示例。
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| badgenumber | checktype | recordout | checkdate | employeeidno | fullname | departmentname |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-25 14:00:02.000 | 2019-12-25 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-25 15:39:57.000 | 2019-12-25 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-25 22:39:57.000 | 2019-12-25 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
2个时间戳相差不到3小时。因此 3:39:57 PM 将被选中,而 10:39:57 PM 将被忽略并保留在 table 因为它与同一列中的其他 2 个数据相比超过 3 小时。
这是我的预期输出。
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| badgenumber | checktype | recordout | checkdate | employeeidno | fullname | departmentname |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-21 23:43:36.000 | 2019-12-21 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-22 22:36:50.000 | 2019-12-22 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-23 18:03:16.000 | 2019-12-23 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-24 22:06:58.000 | 2019-12-24 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-25 14:00:02.000 | 2019-12-25 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-25 15:39:57.000 | 2019-12-25 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-25 22:39:57.000 | 2019-12-25 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-26 14:00:02.000 | 2019-12-26 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-28 22:00:01.000 | 2019-12-28 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-28 23:31:11.000 | 2019-12-28 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-29 15:08:10.000 | 2019-12-29 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2019-12-30 16:03:20.000 | 2019-12-30 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2020-01-02 06:52:18.000 | 2020-01-02 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2020-01-03 08:00:57.000 | 2020-01-03 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
| 1233 | O | 2020-01-04 06:40:11.000 | 2020-01-04 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
这是我需要它工作的代码部分。
(SELECT MAX(userinfo.badgenumber) AS badgenumber, MAX(RTRIM(checkinout.checktype)) AS 'checktype',
MAX(checkinout.checktime) as 'recordout', MAX(CONVERT(date,checkinout.checktime)) as checkdate,
MAX(RTRIM(employeemasterfile.employeeidno)) AS 'employeeidno', MAX(RTRIM(employeemasterfile.lastname))+', '+
MAX(RTRIM(employeemasterfile.firstname))+' '+MAX(LEFT(employeemasterfile.middlename,1))+'.' AS 'fullname',
MAX(RTRIM(departmentmasterfile.departmentname)) AS 'departmentname' FROM ((checkinout INNER JOIN userinfo
ON checkinout.userid = userinfo.userid) INNER JOIN employeemasterfile ON userinfo.badgenumber = employeemasterfile.fingerscanno)
INNER JOIN departmentmasterfile ON LEFT(employeemasterfile.employeeidno, 4) = LEFT(departmentmasterfile.departmentcode, 4)
WHERE CONVERT(date,checkinout.checktime) BETWEEN '2019-12-21' AND DATEADD(DAY, 1,'2020-01-05') AND fingerscanno = '1233'
AND CHECKINOUT.CHECKTYPE = 'O' COLLATE SQL_Latin1_General_CP1_CS_AS GROUP BY userinfo.badgenumber, LEFT(checkinout.checktime,14)) AS t2
ON
t2.recordout BETWEEN DATEADD(HOUR,-6,t0.mergetimeoutorig) AND DATEADD(HOUR, 6,t0.mergetimeoutorig)
Example value
t0.mergetimeoutorig
= 25/12/2019
以下代码将为您提供需要排除的日期。
SELECT DS.*
FROM @DataSource DS1
CROSS APPLY
(
SELECT *
FROM @DataSource DS2
WHERE DS1.[checkdate] = DS2.[checkdate]
AND DATEDIFF(HOUR, DS1.[recordout], DS2.[recordout]) < 3
AND DS1.[recordout] < DS2.[recordout]
) DS;
然后你可以DELETE
从原来的table或SELECT
中只需要数据。这是完整的工作示例:
DECLARE @DataSource TABLE
(
[recordout] DATETIME
,[checkdate] DATE
);
INSERT INTO @DataSource ([recordout], [checkdate])
VALUES ('2019-12-21 23:43:36.000', '2019-12-21')
,('2019-12-22 22:36:50.000', '2019-12-22')
,('2019-12-23 18:03:16.000', '2019-12-23')
,('2019-12-24 22:06:58.000', '2019-12-24')
,('2019-12-25 01:39:57.000', '2019-12-25')
,('2019-12-25 02:39:57.000', '2019-12-25')
,('2019-12-25 02:49:57.000', '2019-12-25')
,('2019-12-25 14:00:02.000', '2019-12-25')
,('2019-12-25 15:39:57.000', '2019-12-25')
,('2019-12-25 22:39:57.000', '2019-12-25')
,('2019-12-26 14:00:02.000', '2019-12-26')
,('2019-12-28 22:00:01.000', '2019-12-28')
,('2019-12-28 23:31:11.000', '2019-12-28')
,('2019-12-29 15:08:10.000', '2019-12-29')
,('2019-12-30 16:03:20.000', '2019-12-30')
,('2020-01-02 06:52:18.000', '2020-01-02')
,('2020-01-03 08:00:57.000', '2020-01-03')
,('2020-01-04 06:40:11.000', '2020-01-04');
WITH DataSource AS
(
SELECT DS.*
FROM @DataSource DS1
CROSS APPLY
(
SELECT *
FROM @DataSource DS2
WHERE DS1.[checkdate] = DS2.[checkdate]
AND DATEDIFF(HOUR, DS1.[recordout], DS2.[recordout]) < 3
AND DS1.[recordout] < DS2.[recordout]
) DS
)
SELECT DS1.*
FROM @DataSource DS1
LEFT JOIN DataSource DS2
ON DS1.[checkdate] = DS2.[checkdate]
AND DS1.[recordout] = DS2.[recordout]
WHERE DS2.[checkdate] IS NULL
ORDER BY DS1.[recordout] ASC;
尝试在您的查询中实现以下逻辑
--Table where swipe details are stored
Create table #EmployeeSwipe
(
ID INT IDENTITY(1,1) primary key,
EmployeeID INT,
CheckType varchar(10),
SwipeTime DATETIME,
SwipeDate DATE
)
GO
--this table will have filtered row id's
CREATE Table #tempdata
(
RowID INT
)
GO
--Dump data
Insert into #EmployeeSwipe values(
1,'I','2019-12-27 10:43:36.000','12/27/2019'
)
Insert into #EmployeeSwipe values(
1,'O','2019-12-27 14:43:36.000','12/27/2019'
)
Insert into #EmployeeSwipe values(
1,'O','2019-12-27 15:43:36.000','12/27/2019'
)
Insert into #EmployeeSwipe values(
1,'O','2019-12-27 22:43:36.000','12/27/2019'
)
Insert into #EmployeeSwipe values(
2,'O','2019-12-27 14:43:36.000','12/27/2019'
)
Insert into #EmployeeSwipe values(
2,'O','2019-12-27 15:43:36.000','12/27/2019'
)
Insert into #EmployeeSwipe values(
3,'O','2019-12-27 14:43:36.000','12/27/2019'
)
GO
;with cteres
AS
(
SELECT
P.ID,P.EmployeeID ,P.SwipeDate ,
p.SwipeTime,
LEAD(p.SwipeTime) OVER(Partition By P.EmployeeID ORDER BY P.ID) NextValue
FROM #EmployeeSwipe P Where checktype='O'
)
--Here check if difference between two swap time is more than 3 hours
Insert into #tempdata
select ID from
(select CASE WHEN
DATEDIFF(HOUR,SwipeTime,NextValue) > 3 THEN 1
WHEN DATEDIFF(HOUR,SwipeTime,NextValue) IS NULL THEN 1
ELSE 0 END as IsValid,ID from cteres
) --where NextValue is not null)
mres where mres.IsValid = 1
--This will give you final output
select ES.* from #tempdata T JOIN #EmployeeSwipe ES ON T.RowID = ES.ID