I have a t1 table with duplicates. I would like to identify these duplicates by putting a flag on them.
- I would like labeled all the lines whose idcommande is unique as "keep"
- For the same idCommand, we set the one with status = ‘in progress’ AND typecom = ‘canceled’ to "keep"
- For the same idcommand, we set to keep those which have an idRef, if idRef is not null.
The flag can be of any type True or False or other
CREATE TABLE t1 (
id SERIAL PRIMARY KEY,
idcommande VARCHAR(50),
date_ DATE,
typecom VARCHAR(50),
statut VARCHAR(20),
idref INT
);
INSERT INTO t1 (idcommande, date_, statut, typecom, idref) VALUES
('1001', '2024-09-15', 'in progress', 'in progress', 1), -- don't want
('1001', '2024-09-15', 'in progress', 'canceled', 2), -- keep
('1002', '2024-09-16', 'completed', 'sent', 3), -- keep
('1003', '2024-09-16', 'completed', 'sent', NULL), -- don't want
('1003', '2024-09-17', 'completed', 'sent', 4); -- keep
('1004', '2024-09-15', 'completed', 'canceled', 5), -- keep
I tried to do this but I don’t really understand how to use partition. The lines with rownum at 1 are kept but the line with idref at null is also kept even though I don’t want it
WITH RankedResults AS (
select *,
ROW_NUMBER() OVER (
PARTITION BY t1.idcommande
ORDER BY
CASE
WHEN t1.statut = 'in progress' and t1.typeCom != 'canceled' THEN 1
WHEN t1.idref IS NULL THEN 1
ELSE 0
END,
t1.date_,
t1.typecom
) AS RowNum
FROM (
SELECT *
FROM t1
) as t1
)
select * from (SELECT * FROM RankedResults) as tmain
2
Answers
Salut @dia05!
CASE
statement:t1.typecom = 'canceled'
(instead of!= 'canceled'
)idref
IS NOT NULL
to prioritize rows whereidref
is present (instead ofidref IS NULL
).ELSE 3
Output:
hope this would help
I would suggest checking the case statement the typeCom should by =’cancel’ instead of != :
I think this query should help you:
I separated each criteria on its own condition then prioritize the data on the order by statement
Output:
enter image description here