问题描述
我目前有这张桌子:
名字 | 姓氏 |
---|---|
简 | Doe |
约翰 | 史密斯 |
鲍勃 | 史密斯 |
爱丽丝 | 史密斯 |
并且我希望让表在姓氏中查找重复项并将值返回到新列中并排除任何空/唯一值,如下表所示,或在第三列中返回是/否.
名字 | 姓氏 | 重复 |
---|---|---|
简 | Doe | 0 |
约翰 | 史密斯 | 3 |
鲍勃 | 史密斯 | 3 |
爱丽丝 | 史密斯 | 3 |
或
名字 | 姓氏 | 重复 |
---|---|---|
简 | Doe | 否 |
约翰 | 史密斯 | 是 |
鲍勃 | 史密斯 | 是 |
爱丽丝 | 史密斯 | 是 |
当我尝试将查询输入 Access 数据库时,我不断收到运行时 3141 错误。
我为获得第一个选项而尝试的代码是: SELECT first_name,last_name,COUNT (last_name) AS Duplicates 发件人表 GROUP BY last_name,first_name 有 COUNT(last_name)=>0
解决方法
您可以使用子查询。但我会推荐 1
而不是 0
:
select t.*,(select count(*)
from t as t2
where t2.last_name = t.last_name
)
from t;
如果您真的想要零而不是 1,那么一种方法是:
select t.*,(select iif(count(*) = 1,count(*))
from t as t2
where t2.last_name = t.last_name
)
from t;