【问题标题】:How to delete duplicated data by column form database [duplicate]如何从数据库中按列删除重复数据[重复]
【发布时间】:2019-07-28 04:15:38
【问题描述】:

我有一个表,其中一列有多个重复的行。所以我想清除重复的数据。

事件表:

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。我想删除 23 但不是 5。因为5是最后一次更新。

【问题讨论】:

  • 最后按 id 或日期?
  • event_id 是增量的,也是数据的。没关系。

标签: sql postgresql duplicates sql-delete postgresql-9.5


【解决方案1】:

存在:

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
  )

【讨论】:

    【解决方案2】:

    我会把它写成存在:

    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';
    

    【讨论】:

      猜你喜欢
      • 2014-06-01
      • 1970-01-01
      • 2013-04-12
      • 2013-10-07
      • 2012-07-18
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      • 2016-09-04
      相关资源
      最近更新 更多