我有一个查询,当前发现重复,它工作正常。问题是某些记录有一个名为DS的字段,它可以有两个可能的值。 VOTER或DMV。
在查找重复项时,如果找到重复项,我想删除VOTER记录而不是DMV记录。这可以轻松完成吗?
WITH cte
AS (SELECT *,
Row_Number() OVER(partition BY fips_county_code, last, suffix, first, birthdate Order by (select null)) AS Rn
FROM WORK)
UPDATE cte
SET DUPES = 'D'
WHERE RN > 1;
答案 0 :(得分:1)
您可以更改order by
中的row_number()
以包含case
表达式,以便'DMV'
具有更高优先级,如下所示:
;with cte as (
select *
, rn = row_number() over(
partition by fips_county_code, last, suffix, first, birthdate
order by case when ds = 'dmv' then 0 else 1 end asc
)
from work
)
update cte
set dupes = 'D'
where rn > 1;
答案 1 :(得分:0)
将DS放入您的order by子句中。 DMV出现在Voter之前。如果两者都存在,则DVM将是RN 1,选民将是RN 2。
WITH cte
AS (SELECT *,
Row_Number() OVER(partition BY fips_county_code, last, suffix, first, birthdate Order by DS ) AS Rn
FROM WORK)
UPDATE cte
SET DUPES = 'D'
WHERE RN > 1;