我希望将潜在重复项的数量作为列添加到表中。因此,我需要计算具有相同属性(first_name,last_name,birthyear)的人数,并将它们视为可能的重复项。
我可以通过对它们进行分组来输出潜在重复数量:
select first_name, last_name, birthyear, COUNT(*) -1 as duplicates
from people
group by first_name, last_name, birthyear
having birthyear is not null;
如果缺少分娩信息,则不会将其视为副本。
但是我怎么能添加结果'重复'列表来源表'人物'吗
答案 0 :(得分:1)
在update命令中将查询用作派生表(FROM
子句中的子查询):
alter table people add duplicates int;
update people p
set duplicates = s.duplicates
from (
select first_name, last_name, birthyear, count(*) -1 as duplicates
from people
where birthyear is not null
group by first_name, last_name, birthyear
) s
where s.first_name = p.first_name
and s.last_name = p.last_name
and s.birthyear = p.birthyear;