[英]SQL get row value where count of rows for that row value matches with of the column of that row-value
I have table in oracle db as follow. 我在oracle db中有如下表。
ID | TOTAL_IDS
----+----------
A1 + 2
A1 + 2
B1 + 1
C1 + 3
C1 + 3
C1 + 3
D1 + 2
I want list of IDs where count of distinct IDs matches with TOTAL_IDs. 我想要ID列表,其中不同ID的数量与TOTAL_ID相匹配。 For eg.
例如。 count of ID A1 is 2 and it matches TOTAL_IDS column.
ID A1的计数为2,并且与TOTAL_IDS列匹配。
So my query should return A1,B1,C1. 因此,我的查询应返回A1,B1,C1。
Try Having clause : http://sqlfiddle.com/#!4/06eed6/9 尝试Have子句: http : //sqlfiddle.com/#!4/06eed6/9
min=max
==> all row have the same value (group by IDS) min=max
==>所有行都具有相同的值(按IDS分组)
min=count
==> your expected citeria min=count
==>您期望的条件
select
IDS
from
your_table
group by
IDS
having
min(TOTAL_IDS) = count(*) and
max(TOTAL_IDS) = min(TOTAL_IDS)
return : 返回:
| IDS |
|-----|
| A1 |
| B1 |
| C1 |
select distinct id
from tablename t1
where TOTAL_IDS = (select count(*) from tablename t2
where t2.id = t1.id)
Will not detect inconsistent table data, eg if ('D1', 4) or ('D2',null) is added to the table. 将不会检测到不一致的表数据,例如,如果将('D1',4)或('D2',null)添加到表中。
select ID,count(ID) from table group by ID having count(ID)=count(TOTAL_IDS);
try: 尝试:
SELECT ID, TOTAL_IDS
FROM tb
GROUP BY ID, TOTAL_IDS
HAVING COUNT(ID) = TOTAL_IDS
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.