我必须编写一个查询来从表中删除重复的活动条目。以下是表格中的一组示例行。
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
我必须删除带有max(时间戳)且active = 1的行。可能有几个这样的行。我尝试了以下查询,但它不起作用。有人可以帮忙。
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)