删除 SQL 中特定列的重复项
Remove duplicate in SQL with specfic column
我的要求是删除 sql 查询中具有特定列的重复项(即类似于 MS Excel 中的删除重复项选项)
例如:我的查询 o/p 有 8 列,但有 3 列要删除。有以下8列
date,name, dept_name,city,phne_no,country,gender,age
我需要删除重复项 w.r.t
name, city, phne_no
这在 sql 中可行吗?请注意,我无权删除 table 中的数据,因此我只能提取。请帮助我。
例如,如果另一行具有相同的名称、城市和 phne_no EXISTS
,但日期较新,则可以 delete
一行:
delete from tablename t1 where exists (select 1 from tablename t2
where t1.name = t2.name
and t1.city = t2.city
and t1.phne_no = t2.phne_no
and t2.date > t1.date)
编辑:
如果您只想 select
较新的行,请使用 NOT EXISTS
查找没有任何较新的行 "duplicates":
select *
from tablename t1
where not exists (select 1 from tablename t2
where t1.name = t2.name
and t1.city = t2.city
and t1.phne_no = t2.phne_no
and t2.date > t1.date)
我的要求是删除 sql 查询中具有特定列的重复项(即类似于 MS Excel 中的删除重复项选项)
例如:我的查询 o/p 有 8 列,但有 3 列要删除。有以下8列
date,name, dept_name,city,phne_no,country,gender,age
我需要删除重复项 w.r.t
name, city, phne_no
这在 sql 中可行吗?请注意,我无权删除 table 中的数据,因此我只能提取。请帮助我。
例如,如果另一行具有相同的名称、城市和 phne_no EXISTS
,但日期较新,则可以 delete
一行:
delete from tablename t1 where exists (select 1 from tablename t2
where t1.name = t2.name
and t1.city = t2.city
and t1.phne_no = t2.phne_no
and t2.date > t1.date)
编辑:
如果您只想 select
较新的行,请使用 NOT EXISTS
查找没有任何较新的行 "duplicates":
select *
from tablename t1
where not exists (select 1 from tablename t2
where t1.name = t2.name
and t1.city = t2.city
and t1.phne_no = t2.phne_no
and t2.date > t1.date)