我想删除 postgresql 中的重复条目。没有唯一约束,但我想将所有列放在一起考虑将一行视为重复。
我在表中的列名称为: Date (Date), Name (text), Age (integer) , Score (double)
我想将所有列一起考虑以将其标识为重复项。如何在 Postgresql 中实现这一点。
PostgreSQL 分配了一个ctid伪列来标识每一行的物理位置。您可以使用它来识别具有相同值的不同行:
-- Create the table
CREATE TABLE my_table (num1 NUMERIC, num2 NUMERIC);
-- Create duplicate data
INSERT INTO my_table VALUES (1, 2);
INSERT INTO my_table VALUES (1, 2);
-- Remove duplicates
DELETE FROM my_table
WHERE ctid IN (SELECT ctid
FROM (SELECT ctid,
ROW_NUMBER() OVER (
PARTITION BY num1, num2) AS rn
FROM my_table) t
WHERE rn > 1);
Run Code Online (Sandbox Code Playgroud)