sql-根据时间戳删除重复项

wa7juj8i  于 2021-07-26  发布在  Java
关注(0)|答案(3)|浏览(288)

我的问题是有一个历史表,它每天提取一个表并给它一个时间戳。不幸的是,数据在过去每天被加载多次,这是不应该的。
就像:
时间戳/id
13.07.2020 15:01.../123
13.07.2020 15:02.../123
13.07.2020 15:03.../123
14.07.2020 15:01.../123
14.07.2020 15:02.../123
14.07.2020 15:03.../123
应该是这样的:
13.07.2020 15:01.../123
14.07.2020 15:01.../123
我正在寻找一种方法来删除每天的第一个时间戳的基础上重复。
你有没有办法用这种方法删除重复的内容?
提前谢谢!

jtoj6r0c

jtoj6r0c1#

我建议使用cte删除:

WITH cte AS (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY id, CONVERT(date, ts_col) ORDER BY ts_col) rn
    FROM yourTable
)

DELETE
FROM cte
WHERE rn > 1;     -- targets all records per day except for the first one
5hcedyr0

5hcedyr02#

如果只有两列,请使用聚合:

select id, cmin(timestamp) as timestamp
from t
group by id, convert(date, timestamp);

如果您有许多列并且想要完整的行,那么 row_number() 可能是最好的选择:

select t.*
from (select t.*,
             row_number() over (partition by id, convert(date, timestamp) order by timestamp) as seqnum
      from t
     ) t
where seqnum = 1;
cyvaqqii

cyvaqqii3#

您可以使用此选择来控制:

select  a.* from yourtable a
inner join
(
    select id,convert(date,[datetime]) [date], MIN([datetime]) [datetime]
    from yourtable
    group by id,convert(date,[datetime])
) b on a.id = b.id and convert(date,a.[datetime]) = b.[date] and a.[datetime] <> b.[datetime]

然后删除:

delete  a from yourtable a
inner join
(
    select id,convert(date,[datetime]) [date], MIN([datetime]) [datetime]
    from yourtable
    group by id,convert(date,[datetime])
) b on a.id = b.id and convert(date,a.[datetime]) = b.[date] and a.[datetime] <> b.[datetime]

相关问题