从4列中选择:ID,许可,代码,ABBR
在剩余的列中,ID可以使用不同的数据出现多次。我想排除ID仅出现一次的所有记录。
这是用于MS Access 2016。
SELECT ID, LICENSE, CODE, ABBR
FROM tblLicense
WHERE CODE IN ("A", "B", "C")
GROUP BY ID, LICENSE, CODE, ABBR
HAVING COUNT(ID)>1;
如果我运行一个不带计数的简单选择查询,我会看到ID仅出现一次的记录,以及ID多次出现的记录。但是,当我添加COUNT()时,我得到了零条记录。我希望它排除ID出现多次的所有记录。不确定如何纠正此问题。非常感谢您的帮助。
答案 0 :(得分:2)
您可以尝试对计数为1的ID使用NOT IN
select ID, LICENSE, CODE, ABBR
from
FROM tblLicense
WHERE CODE IN ("A", "B", "C")
AND id not in (
select id
from tblLicense
WHERE CODE IN ("A", "B", "C")
group by id
having count(*) = 1
)
答案 1 :(得分:1)
您也可以使用联接来实现此目的,例如:
select t1.id, t1.license, t1.code, t1.abbr
from tbllicense t1 inner join
(
select t2.id
from tbllicense t2
where t2.code in ("A", "B", "C")
group by t2.id
having count(*) > 1
) q on t1.id = q.id
where t1.code in ("A", "B", "C")