我有一个select语句,它返回表中的重复项。我想现在能够删除这些重复项。如何使用ORACLE SQL执行此操作?
SELECT
c1.name AS c1name,
c2.name AS c2name,
c1.cy_code AS c1country,
c2.cy_code AS c2country,
c1.latitude AS c1lat,
c1.longitude AS c1long,
c2.latitude AS c2lat,
c2.longitude AS c2long,
calc_distance(c1.latitude,
c1.longitude,
c2.latitude,
c2.longitude) AS distance
FROM city_temp c1,
city_temp c2
WHERE c2.name = c1.name
AND c2.cy_code = c1.cy_code
AND calc_distance(c1.latitude, c1.longitude, c2.latitude, c2.longitude) = 0
AND c1.latitude = c2.latitude
AND c1.longitude = c2.longitude;
答案 0 :(得分:1)
如果你可以改变功能calc_distance
,你可以尝试这个DELETE FROM city_temp
WHERE rowid not in
(SELECT MIN(rowid)
FROM city_temp
WHERE calc_distance(latitude, longitude)=0
GROUP BY name, cy_code, latitude,longitude
)
答案 1 :(得分:1)
您将使用exists子句删除所有与较高rowid相等的行:
delete from city_temp c1
where exists
(
select *
from city_temp c2
where c2.name = c1.name
and c2.cy_code = c1.cy_code
and c2.latitude = c1.latitude
and c2.longitude = c1.longitude
and calc_distance(c1.latitude, c1.longitude, c2.latitude, c2.longitude) = 0
and c2.rowid > c1.rowid
);
答案 2 :(得分:0)
使用rowid,您可以执行此操作
DELETE FROM city_temp where rowid in (
SELECT c1.rowid
FROM city_temp c1, city_temp c2
WHERE c2.name = c1.name and c2.cy_code = c1.cy_code and calc_distance(c1.latitude, c1.longitude, c2.latitude, c2.longitude)=0
and c1.latitude = c2.latitude and c1.longitude = c2.longitude)
答案 3 :(得分:0)
删除重复行的经典方法是
DELETE FROM table_name A WHERE ROWID > (
SELECT min(rowid) FROM table_name B
WHERE A.key_values = B.key_values)
只需将SELECT语句插入其中即可。
或者对于大型数据集的最高速度 - 您可以将不同的数据复制到临时表中,截断原始数据,复制数据并删除临时表。