我有一个这样的表:
t1 t2 t3
1 01 M6
1 02 M6
2 01 M1
2 01 M1
2 02 M1
3 04 M4
3 04 M4
4 01 M1
4 01 M2
我需要结束这样的事情:
t1 t2 t3
1 01 M6
1 02 M6
2 01 M1
2 02 M1
3 04 M4
4 01 M1
4 01 M2
换句话说,我需要:
对于每个t1,删除在t2上重复的行(保留其中一个),同时具有相同的t3值。如果它们在t2上重复但具有不同的t3,则不会被删除。
正在使用类似这样的东西进行测试,但是还不足够:
WITH cte AS (
SELECT t1,
RNum = ROW_NUMBER() OVER (PARTITION BY t2, t1 order by t3)
FROM tbl
)
DELETE FROM cte WHERE RNum > 1;
答案 0 :(得分:0)
Execute a query like this:
DELETE from tbl WHERE ROWID in (
SELECT DISTINCT dup.ROWID
FROM tbl inner join tbl as dup ON tbl.t1 = dup.t1 AND tbl.t2 = dup.t2 AND tbl.t3 = dup.t3 AND dup.ROWID > tbl.ROWID
)
答案 1 :(得分:0)
具有CTE:
with cte as (
select
t1, t2, t3,
ROW_NUMBER() OVER (PARTITION BY t1, t2, t3 order by t1, t2, t3) rn from tbl
)
delete from cte
where rn > 1;
请参见demo
答案 2 :(得分:-1)
请在下面尝试。希望它会有所帮助。
delete from a
from
(select t1, t2, t3
,ROW_NUMBER() over (partition by t1, t2, t3
order by t1, t2, t3) RowNumber
from tbl) a
where a.RowNumber > 1