我有一张非常大的桌子(几百万条记录)。有些记录有重复(基于FieldA),唯一的区别是FiedldB中的值。我想创建一个查询,根据FieldA删除所有重复记录,保持FieldB中值最小的记录。这可能吗?
答案 0 :(得分:0)
似乎很容易提取这些值:
select distinct a,
min(b) b
from t
group by a;
小提琴,例如:http://sqlfiddle.com/#!9/bc4c9/3
您应该能够从中调整删除方法。
答案 1 :(得分:0)
CREATE TABLE TABLE1
(
FieldA VARCHAR2(30),
FieldB VARCHAR2(30),
FieldC VARCHAR2(30)
);
INSERT INTO TABLE1 VALUES
('DUMMYDATA-A1','DUMMYDATA-B1','DUMMYDATA-C1'
);
INSERT INTO TABLE1 VALUES
('DUMMYDATA-A1','DUMMYDATA-B4','DUMMYDATA-C1'
);
INSERT INTO TABLE1 VALUES
('DUMMYDATA-A1','DUMMYDATA-B3','DUMMYDATA-C1'
);
INSERT INTO TABLE1 VALUES
('DUMMYDATA-A1','DUMMYDATA-B2','DUMMYDATA-C1'
);
COMMIT;
SELECT FieldA,
FieldB,
FieldC,
RANK() OVER( PARTITION BY FieldA ORDER BY FieldB ASC) AS COLUMN_ALIAS
FROM TABLE1; --IDENTIFIES DUPLICATES BASED ON RANK VALUE
---PERFORM DELETE
DELETE
FROM TABLE1
WHERE ROWID IN
(SELECT ROWID
FROM
(SELECT ROWID,
RANK() OVER( PARTITION BY FieldA ORDER BY FieldB ASC) AS COLUMN_ALIAS
FROM TABLE1
)
WHERE COLUMN_ALIAS>1
);
COMMIT;
SELECT * FROM TABLE1; -- CONTAINS A SINGLE RECORD
RANK函数可以识别重复记录并便于仅删除重复记录,保留原始行。这已经在这里讨论过:Deleting duplicates rows from oracle。希望这有帮助
然而,由于DELETE本身较慢,因此可以在INSERT中实现适当的约束(包含数百万条记录)以避免重复输入。