我的问题是,有一个历史记录表,它每天提取一张表并为其提供时间戳。不幸的是,过去每天都多次加载数据,而实际上并非如此。
就像:
并且应该像这样:
我正在寻找一种基于每天的第一个时间戳删除重复项的方法。
您有任何想法以这种方式删除重复项吗?
提前谢谢!
答案 0 :(得分:4)
我建议使用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
答案 1 :(得分:0)
如果只有两列,请使用聚合:
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;
答案 2 :(得分:0)
您可以使用此选择来控制:
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]