选择同一行中的顶行数据(如果同一列中的数据差小于';n';小时)sql
我有需要排序的现有数据,我的问题是如何对同一列中的数据进行排序,如果值之间的差异小于3小时,将选择最高值 这可能吗 根据下表,2019-12-25有3个值选择同一行中的顶行数据(如果同一列中的数据差小于';n';小时)sql,sql,sql-server,tsql,sql-server-2012,Sql,Sql Server,Tsql,Sql Server 2012,我有需要排序的现有数据,我的问题是如何对同一列中的数据进行排序,如果值之间的差异小于3小时,将选择最高值 这可能吗 根据下表,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-1
| 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将保留在表中,因为与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 | | | |
+-------------+-----------+-------------------------+------------+--------------+----------+----------------+
两个时间戳之间的差异小于3小时。因此,将改为选择3:39:57 PM,而10:39:57 PM将被忽略,并将保留在表中,因为与同一列中的其他两个数据相比,其时间超过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)
示例值t0.mergetimeoutorig
=2019年12月25日
以下代码将为您提供需要排除的日期
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;
尝试在查询中实现以下逻辑
--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
非常感谢。我将在我的查询中实现这个逻辑。谢谢。我也会检查一下。
--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