【问题标题】:Remove duplicate rows in sql database删除sql数据库中的重复行
【发布时间】:2020-08-03 06:21:28
【问题描述】:

我有一个包含以下字段的表格:

`contract`, `title`, `category`, `reglementation`, `company`, `role`, `start_date`, `end_date`, `creation_date`, `update_date`, `created_by`, `updated_by`,`context`,`hash`,`accept_schedule`,`need_timecard`

并且,我需要删除具有相同合同且 end_date 为 null 的行,但 start_date 的值较小

此查询返回重复行,它返回 9000

select contract, count(*) 
from n_h_associate_occupation o 
where end_date is null 
group by o.contract 
having count(*) > 1;

有没有办法删除这些行?请帮忙

【问题讨论】:

  • 样本数据和期望的结果会有所帮助。

标签: java mysql sql duplicates row


【解决方案1】:

这是一个适用于所有 MySQL 版本的选项:

SELECT o1.*
FROM n_h_associate_occupation o1
INNER JOIN
(
    SELECT contract, MIN(start_date) AS min_start_date
    FROM n_h_associate_occupation
    WHERE end_date IS NULL
    GROUP BY contract
) o2
    ON o1.contract = o2.contract AND
       o1.start_date = o2.min_start_date
WHERE
    o1.end_date IS NULL;

这假设您希望保留具有最短开始日期的行,如果给定合同有多个行。

【讨论】:

    【解决方案2】:

    如果对于contract 的每个值,您想删除所有具有最小start_date 值的行,即只保留start_date 最大的行,对于那些end_date 为@ 的行987654326@,然后:

    delete o from
    n_h_associate_occupation o join
    ( 
    select contract, max(start_date) as max_start_date
    from n_h_associate_occupation
    where end_date is null
    group by contract
    having count(*) > 1
    ) sq
    on o.contract = sq.contract and o.end_date is null and o.start_date <> sq.max_start_date
    

    See Db Fiddle

    如果您想保留 最小 start_date 的行(从您的问题中并不完全清楚),那么:

    delete o from
    n_h_associate_occupation o join
    ( 
    select contract, min(start_date) as min_start_date
    from n_h_associate_occupation
    where end_date is null
    group by contract
    having count(*) > 1
    ) sq
    on o.contract = sq.contract and o.end_date is null and o.start_date <> sq.min_start_date
    

    【讨论】:

      猜你喜欢
      • 2013-10-07
      • 2017-04-25
      • 1970-01-01
      • 1970-01-01
      • 2015-07-02
      • 2012-01-01
      • 2014-06-01
      • 2014-05-09
      • 2018-09-01
      相关资源
      最近更新 更多