我有一个表,它有一个列的多个重复行。所以我想清除重复的数据。
活动表:
event_id, event_type, data_id, data date
1 insert 1 x 06.03.2019 13:04
2 update 1 x1 06.03.2019 13:05
3 update 1 x11 06.03.2019 13:06
4 insert 2 y 06.03.2019 13:07
5 update 1 x111 06.03.2019 13:08
6 delete 1 x111 06.03.2019 13:09
7 update 2 y1 06.03.2019 13:10
8 update 2 y11 06.03.2019 13:11
9 update 2 y11 06.03.2019 13:12每个数据id在表中都有1插入、N更新和1删除事件行。所以我想删除N-1更新事件,但是最后一个事件不会被删除。例如,在这个表中,对于data_id=1,更新事件是2,3,5。我想删除2和3,但不想删除5。因为5是最后一次更新。
发布于 2019-03-06 13:26:00
与存在:
delete from tablename t
where
event_type = 'update'
and exists (
select 1 from tablename
where
data_id = t.data_id
and
event_type = 'update'
and
event_id > t.event_id
)发布于 2019-03-06 13:25:35
我会把这写成是存在的:
DELETE
FROM your_table t1
WHERE EXISTS (SELECT 1 FROM your_table t2
WHERE t1.data_id = t2.data_id AND
t2.event_id < t1.event_id AND
t2.event_type = 'update') AND
t1.event_type = 'update';https://stackoverflow.com/questions/55024104
复制相似问题