如果在7天内删除重复数据

问题描述 投票:0回答:2

以下是我的表格和样本数据

DECLARE @Employee_Log table(ID int,eid int, ecode varchar(100), emp_startdate date)



  INSERT INTO @Employee_Log
SELECT 1, 1, 'aaa','2019-01-01'
UNION ALL
SELECT 2, 1, 'aaa','2019-01-05'
UNION ALL
SELECT 3, 1, 'bbb','2019-01-03'
UNION ALL
SELECT 4, 2, 'aaa','2019-01-03'
UNION ALL
SELECT 5, 1, 'aaa','2019-02-01'
UNION ALL
SELECT 6, 1, 'aaa','2019-02-15'
UNION ALL
SELECT 7, 1, 'aaa','2019-02-19'
UNION ALL
SELECT 8, 1, 'aaa','2019-02-28'

在上面的数据中,我想删除基于eidecode的重复。如果emp_startdate在7天内,则获取最新数据并忽略其余数据。

我尝试了以下代码,但如何添加周范围的条件检查

SELECT 
ROW_NUMBER() OVER(PARTITION BY eid,ecode ORDER BY emp_startdate desc) as rownum,
ID,eid,ecode,emp_startdate
FROM @Employee_Log

我想要的结果如下所示

  ID    eid ecode   emp_startdate
    2   1   aaa      2019-01-05
    5   1   aaa      2019-02-01
    4   2   aaa      2019-01-03
    7   1   aaa      2019-02-19
    8   1   aaa      2019-02-28
    3   1   bbb      2019-01-03
sql-server tsql
2个回答
1
投票

如果在同样的7天内发生超过2个事件,我仍然不确定你想要发生什么。但是这个解决方案将获得所有系列日期的最新日期,其中日期之间的差异为7天或更短。

select ID,eid,ecode,emp_startdate
from
(
select ID,
eid,
ecode,
emp_startdate,
datediff(day
    ,emp_startdate
    ,lead(emp_startdate) 
        over
        (partition by eid,ecode order by emp_startdate)) l 
from  @Employee_Log
) a
where l is null or l>7

ID     eid     ecode     emp_startdate
--     ---     -----     -------------
3       1       bbb       2019-01-03
2       1       aaa       2019-01-05
5       1       aaa       2019-02-01
7       1       aaa       2019-02-19
8       1       aaa       2019-02-28
4       2       aaa       2019-01-03

0
投票

以下查询将为您提供您在问题中要求使用简明英语的内容,但您的示例数据和所需输出与您自己的问题相矛盾:

SELECT *
FROM
(
    SELECT
        ROW_NUMBER() OVER (PARTITION BY eid , ecode , YEAR(emp_startdate) 
                                       , DATEPART(WEEK, emp_startdate)
                           ORDER BY emp_startdate DESC
                          ) AS rownum
      , ID
      , eid
      , ecode
      , emp_startdate
    FROM @Employee_Log
) x
WHERE x.rownum = 1;
© www.soinside.com 2019 - 2024. All rights reserved.