我需要根据ID和时间列删除重复的行。我需要保留最新时间的记录。如果有两个记录有最大次数,我可以保留任何记录并删除其中的所有其他记录那个组。请在下面找到我的输入数据
ID TIMES
123 13/01/2018
123 14/01/2018
123 15/01/2018
345 14/01/2018
567 20/01/2018
567 20/01/2018
879 NULL
879 21/01/2018
我已经为同一个写了一个查询。但它没有使用ID = 567的情况,因为它们在时间列中都有相同的值。请在下面找到我的查询
delete FROM table where (ID,times) in(
SELECT ID,times,
RANK() OVER (PARTITION BY ID ORDER BY times DESC NULLS LAST) dest_rank
FROM TABLE
) WHERE dest_rank <> 1
我有什么办法可以做到这一点。
答案 0 :(得分:1)
这是一种方法:
delete t from t
where rowid <> (select max(rowid) keep (dense_rank first order by times desc)
from t t2
where t2.id = t.id
);
但是,我会用临时表来执行此操作:
create temporary table tt
select id, max(times) as times
from t
group by id;
truncate table t;
insert into t(id, times)
select id, times
from tt;
答案 1 :(得分:0)
你可以通过控制或rowid
:
delete mytable where (rowid) in
(
select t1.rowid from mytable t1
where times <
(
select max(times)
from mytable t2
where t2.id = t1.id
and t2.times != t1.times -- for non-matching records of times
)
union all
select t1.rowid from mytable t1
where rowid <
(
select max(rowid)
from mytable t2
where t2.id = t1.id
and t2.times = t1.times -- for matching records of times
)
);
答案 2 :(得分:0)
我会
delete demo where rowid in
( select lag(rowid) over (partition by id order by times nulls first) from demo );
您没有说明如何处理null值。如果要保留具有空日期的行,请将nulls first
更改为nulls last
。