我在oracle db中有表,如下所示。
ID | TOTAL_IDS
----+----------
A1 + 2
A1 + 2
B1 + 1
C1 + 3
C1 + 3
C1 + 3
D1 + 2
我想要ID列表,其中不同ID的计数与TOTAL_ID匹配。例如。 ID A1的计数为2,它与TOTAL_IDS列匹配。
所以我的查询应该返回A1,B1,C1。
答案 0 :(得分:4)
尝试拥有子句:http://sqlfiddle.com/#!4/06eed6/9
min=max
==>所有行都具有相同的值(按IDS分组)
min=count
==>你预期的citeria
select
IDS
from
your_table
group by
IDS
having
min(TOTAL_IDS) = count(*) and
max(TOTAL_IDS) = min(TOTAL_IDS)
返回:
| IDS |
|-----|
| A1 |
| B1 |
| C1 |
答案 1 :(得分:1)
select distinct id
from tablename t1
where TOTAL_IDS = (select count(*) from tablename t2
where t2.id = t1.id)
不会检测不一致的表格数据,例如if('D1',4)或('D2',null)被添加到表中。
答案 2 :(得分:0)
select ID,count(ID) from table group by ID having count(ID)=count(TOTAL_IDS);
答案 3 :(得分:0)
尝试:
SELECT ID, TOTAL_IDS
FROM tb
GROUP BY ID, TOTAL_IDS
HAVING COUNT(ID) = TOTAL_IDS