如果日期差异等于或大于4天或96小时,如何返回重复行

时间:2016-04-13 09:44:38

标签: sql sql-server sql-server-2008 tsql group-by

我必须获取重复记录 如果重复记录之间的日期差异超过96小时或4天,否则忽略具有第一个条目或最早日期的重复条目和返回记录。我的表看起来像这样:

[aAVCaptureMovieFileOutput startRecording]

预期结果:

 .popupBoxWrapper{
     max-width: 550px;
     width: 95%;
     margin: 
     50px auto; 
     text-align: left;
 }

我尝试了以下查询,但它无法正常工作。

ID           SDATE
----------- -----------------------
1           2016-04-13 14:54:18.983
1           2016-04-08 12:55:47.907
2           2016-04-13 14:54:18.983
3           2016-04-13 14:54:18.983
4           2016-04-13 14:54:18.983
5           2016-04-13 14:54:18.983
5           2016-04-11 12:55:47.907
6           2016-04-13 14:54:18.983
6           2016-04-13 14:54:18.983

3 个答案:

答案 0 :(得分:1)

以下查询返回预期结果,添加了内联注释:

-- Simply grouping each ID and get unique row with minimum date
SELECT MIN(SDATE) [SDate], ID
FROM tt
GROUP BY ID

UNION 

-- Get the row with each ID's difference is more than 96 hours
SELECT D.MaxDate  [SDate], D.ID
FROM (
    SELECT MIN(SDATE) [MinDate], MAX(SDATE) [MaxDate], ID
    FROM tt
    GROUP BY ID
) D
WHERE DATEDIFF(HH, D.MinDate, D.MaxDate) >= 96

答案 1 :(得分:0)

请尝试以下查询,我测试过它工作正常。

在忽略列中,您可以更改句点(HH或DAY)

-- drop table #temptbl

WITH tt AS (
SELECT 1 as ID, GETDATE() as SDATE
UNION ALL
SELECT 1 as ID, '2016-04-09 12:55:47.907' as SDATE
UNION ALL
SELECT 2 as ID, GETDATE() as SDATE
UNION ALL
SELECT 3 as ID, GETDATE() as SDATE
UNION ALL
SELECT 4 as ID, GETDATE() as SDATE
UNION ALL
SELECT 5 as ID, GETDATE() as SDATE
UNION ALL
SELECT 5 as ID, '2016-04-11 12:55:47.907' as SDATE
UNION ALL
SELECT 6 as ID, GETDATE() as SDATE
UNION ALL
SELECT 6 as ID, GETDATE() as SDATE
)
SELECT Id,SDATE,case when DATEDIFF(HH,SDATE,GETDATE()) >94 THEN 0 else 1 end AS ignore,
  ROW_NUMBER() OVER ( PARTITION BY tt.ID ORDER BY tt.SDATE desc ) as Rowid
INTO #temptbl
FROM tt 
SELECT Id, sdate   from #temptbl
WHERE (#temptbl.ignore = 0) or (#temptbl.Rowid = 1)

WITH tt AS ( SELECT 1 as ID, GETDATE() as SDATE UNION ALL SELECT 1 as ID, '2016-04-09 12:55:47.907' as SDATE UNION ALL SELECT 2 as ID, GETDATE() as SDATE UNION ALL SELECT 3 as ID, GETDATE() as SDATE UNION ALL SELECT 4 as ID, GETDATE() as SDATE UNION ALL SELECT 5 as ID, GETDATE() as SDATE UNION ALL SELECT 5 as ID, '2016-04-11 12:55:47.907' as SDATE UNION ALL SELECT 6 as ID, GETDATE() as SDATE UNION ALL SELECT 6 as ID, GETDATE() as SDATE ) SELECT Id,SDATE,case when DATEDIFF(HH,SDATE,GETDATE()) >94 THEN 0 else 1 end AS ignore, ROW_NUMBER() OVER ( PARTITION BY tt.ID ORDER BY tt.SDATE desc ) as Rowid INTO #temptbl FROM tt SELECT Id, sdate from #temptbl WHERE (#temptbl.ignore = 0) or (#temptbl.Rowid = 1)

答案 2 :(得分:0)

declare @table table
(
ID int,           SDATE datetime)
insert into @table
(
ID      ,SDATE )
values 
(1,'2016-04-13 14:54:18'),
(1,'2016-04-08 12:55:47'),
(2,'2016-04-13 14:54:18'),
(3,'2016-04-13 14:54:18'),
(4,'2016-04-13 14:54:18'),
(5,'2016-04-13 14:54:18'),
(5,'2016-04-11 12:55:47'),
(6,'2016-04-13 14:54:18'),
(6,'2016-04-13 14:54:18')


;with cte as
(
select  id,min(sdate) mindate,max(sdate) maxdate, datediff(dd,min(sdate),max(sdate)) daysdiff,count(*) as Dups
 from @table
 group by   id 
 )
select  cte.id, t.sdate
from cte
join    @table t on t.id = cte.id 
where   cte.dups > 1 and cte.daysdiff > 4
union all
select  cte.id, 
        mindate
from cte
where   (cte.dups > 1 and cte.daysdiff <= 4) or
        cte.dups = 1