Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 查找特定日期范围内的重复记录_Sql_Sql Server_Sql Server 2008 - Fatal编程技术网

Sql 查找特定日期范围内的重复记录

Sql 查找特定日期范围内的重复记录,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,我有一个表,其中有4列 Serialnvarchar、SIDnvarchar、DateCreatedDate、CIDunique和int 我想找到有重复序列号和SID的记录,以及2个重复序列号介于180天的日期范围之间的记录 请帮忙 样本数据 Serial SID DateCreated CID 02302-25-0036 HONMD01 2017-05-01 00:00:00.000 1 02302-25-0036 HONMD01 2

我有一个表,其中有4列 Serialnvarchar、SIDnvarchar、DateCreatedDate、CIDunique和int 我想找到有重复序列号和SID的记录,以及2个重复序列号介于180天的日期范围之间的记录

请帮忙

样本数据

Serial          SID     DateCreated             CID 
02302-25-0036   HONMD01 2017-05-01 00:00:00.000 1
02302-25-0036   HONMD01 2017-05-01 00:00:00.000 3
0264607         HONMD01 2017-05-01 00:00:00.000 65
0264607         HONMD01 2016-05-01 00:00:00.000 45
03118-09-0366   PRIVA00 2016-05-20 00:00:00.000 34
03118-09-0366   PRIVA00 2016-05-20 00:00:00.000 87
0969130         140439  2017-05-09 00:00:00.000 32
0969130         140439  2017-05-09 00:00:00.000 23
1049567         INIIL00 2017-04-12 00:00:00.000 76
尝试这样做:

with cte as (
    select 
        serial,
        sid,
        dateCreated,
        cid,
        coalesce(max(dateCreated) over(partition by serial, sid order by cid, dateCreated asc rows between unbounded preceding and 1 preceding), '1900-01-01') as last,
        coalesce(min(dateCreated) over(partition by serial, sid order by cid, dateCreated asc rows between 1 following and unbounded following), '5999-01-01') as next
    from table_name
) 
select *
from cte 
where 
    datediff(day, last, dateCreated) >= 180
    and datediff(day, dateCreated, next) >= 180

这是一个具有挑战性的问题!为了便于理解,我在最后的输出中留下了*PreviousDate,rno。以下是我解决问题的方法:

Create table #t(Serial nvarchar(100),SID nvarchar(100),DateCreated date,CID int)
Insert into #t values 
('02302-25-0036',   'HONMD01', '2017-05-01 00:00:00.000', 1),
('02302-25-0036',   'HONMD01', '2017-05-01 00:00:00.000', 3),
('0264607',         'HONMD01', '2017-05-01 00:00:00.000', 65),
('0264607',         'HONMD01', '2016-05-01 00:00:00.000', 45),
('03118-09-0366',   'PRIVA00', '2016-05-20 00:00:00.000', 34),
('03118-09-0366',   'PRIVA00', '2016-05-20 00:00:00.000', 87),
('0969130',         '140439',  '2017-05-09 00:00:00.000', 32),
('0969130',         '140439',  '2017-05-09 00:00:00.000', 23),
('1049567',         'INIIL00', '2017-04-12 00:00:00.000', 76)

Select iq2.*
FROM
 (Select iq.Serial, iq.SID, iq.DateCreated, iq.CID, iq.PreviousDate,
         ROW_NUMBER() OVER (PARTITION BY iq.Serial,iq.SID, CASE WHEN DATEDIFF(day, iq.DateCreated, iq.PreviousDate) <= 180 THEN 1 ELSE 0 END 
                            ORDER BY Serial,SID) rno
  FROM
      (select Serial,SID,DateCreated,CID,
              MAX(DateCreated) OVER (PARTITION BY Serial,SID ORDER BY Serial,SID) maxDate,
              DATEADD(day,-180,MAX(DateCreated) OVER (PARTITION BY Serial,SID ORDER BY Serial,SID)) PreviousDate
       from #t
    )iq
 )iq2 
where iq2.rno <> 1

PS:PreviousDate是最大PreviousDate

请提供一些示例数据和预期输出您想了解当前行是否在过去180天内吗?添加了示例数据。例如,对于序列号02302-25-0036,有两个条目具有相同的SID,并且都在我们需要该数据的180天内。如果两个日期的间隔超过180,我们不希望这样,不要将样本数据添加为图像。很难复制粘贴它谢谢你
Create table #t(Serial nvarchar(100),SID nvarchar(100),DateCreated date,CID int)
Insert into #t values 
('02302-25-0036',   'HONMD01', '2017-05-01 00:00:00.000', 1),
('02302-25-0036',   'HONMD01', '2017-05-01 00:00:00.000', 3),
('0264607',         'HONMD01', '2017-05-01 00:00:00.000', 65),
('0264607',         'HONMD01', '2016-05-01 00:00:00.000', 45),
('03118-09-0366',   'PRIVA00', '2016-05-20 00:00:00.000', 34),
('03118-09-0366',   'PRIVA00', '2016-05-20 00:00:00.000', 87),
('0969130',         '140439',  '2017-05-09 00:00:00.000', 32),
('0969130',         '140439',  '2017-05-09 00:00:00.000', 23),
('1049567',         'INIIL00', '2017-04-12 00:00:00.000', 76)

Select iq2.*
FROM
 (Select iq.Serial, iq.SID, iq.DateCreated, iq.CID, iq.PreviousDate,
         ROW_NUMBER() OVER (PARTITION BY iq.Serial,iq.SID, CASE WHEN DATEDIFF(day, iq.DateCreated, iq.PreviousDate) <= 180 THEN 1 ELSE 0 END 
                            ORDER BY Serial,SID) rno
  FROM
      (select Serial,SID,DateCreated,CID,
              MAX(DateCreated) OVER (PARTITION BY Serial,SID ORDER BY Serial,SID) maxDate,
              DATEADD(day,-180,MAX(DateCreated) OVER (PARTITION BY Serial,SID ORDER BY Serial,SID)) PreviousDate
       from #t
    )iq
 )iq2 
where iq2.rno <> 1
Serial                    SID        DateCreated   CID         PreviousDate   rno
----------               -------     ----------   ----         -----------   ----
02302-25-0036            HONMD01     2017-05-01    3           2016-11-02     2
03118-09-0366            PRIVA00     2016-05-20    87          2015-11-22     2
0969130                  140439      2017-05-09    23          2016-11-10     2