I have to write a query to delete duplicate active entries from the table. Here is a sample set of rows from the table.
select * from balance where ACCOUNT = '832076635';
ACCOUNT BALANCE_AMT TIMESTAMP ACTIVE
832076635 10.23 02-MAR-18 1
832076635 13.34 29-DEC-17 1
832076635 9.22 01-OCT-17 0
I have to delete the row with max(timestamp) with active = 1. There could be several such rows. I tried the below query but it does not work. Could someone please help.
WITH TMP AS
(select account, max(timestamp) AS MAXTIME from balance
where active = 1 group by account having count(*) > 1)
delete from balance b
INNER JOIN TMP t
ON (b.account = t.account
AND b.timestamp = t.MAXTIME)