Implemented removal of duplicate rows as per https://www.oracletutorial.com/advanced-oracle-sql/how-to-delete-duplicate-records-in-oracle/.
However, my situation needs further work. Let's assume that my Table looks like this :
CREATE TABLE fruits
(
fruit_id NUMBER generated BY DEFAULT AS IDENTITY,
fruit_name VARCHAR2(100),
color VARCHAR2(20),
status varchar2(10),
PRIMARY KEY (fruit_id)
);
INSERT INTO fruits(fruit_name, color, status) VALUES ('Apple', 'Red', 'INITIAL');
INSERT INTO fruits(fruit_name, color, status) VALUES ('Apple', 'Red', 'INITIAL');
INSERT INTO fruits(fruit_name, color, status) VALUES ('Orange', 'Orange', 'COMPLETE');
INSERT INTO fruits(fruit_name, color, status) VALUES ('Orange', 'Orange', 'INITIAL');
INSERT INTO fruits(fruit_name, color, status) VALUES ('Orange', 'Orange', 'INITIAL');
INSERT INTO fruits(fruit_name, color, status) VALUES ('Banana', 'Yellow', 'INITIAL');
INSERT INTO fruits(fruit_name, color, status) VALUES ('Banana', 'Green', 'INITIAL');
DELETE
FROM fruits
WHERE fruit_id NOT IN
(
SELECT MAX(fruit_id)
FROM fruits
GROUP BY fruit_name,
color
)
AND STATUS = 'INITIAL';
After deleting the duplicates like above, I still find that one of the duplicate rows(fruit_id =5) still remains.
select * from fruits;
2,Apple,Red,INITIAL
3,Orange,Orange,COMPLETE
5,Orange,Orange,INITIAL
6,Banana,Yellow,INITIAL
7,Banana,Green,INITIAL
I would like to delete all duplicate rows that are in 'INITIAL' state.
How should I go about it ?
UPDATE
Just to be sure, the logic should be : All NON-MAX records in 'INITIAL' state should be deleted. Also, if a record with 'COMPLETE' status is present, then I'd like the duplicate 'INITIAL' record to be deleted as well. In my example, I'd like record with fruit_id = 5(with STATE='INITIAL') to be deleted since there's another record with fruit_id =3(with STATE='COMPLETE') which has the same value of "orange", "orange" but with 'COMPLETE' value.
Completedwith samefruit_nameandcolor?fruit_id = 2should be deleted instead of the one withfruit_id = 1as being non-max for own group offruit_nameandcolor.