[英]Oracle 12c - filter records with multiple values
I have 2 columns: 我有2栏:
DEPT_ID number; DEPT_ID号; DEPT_SUB_ID varchar2(5); DEPT_SUB_ID varchar2(5);
I want to find all the DEPT_ID's which have more than one unique value for DEPT_SUB_ID. 我想找到所有具有多个DEPT_SUB_ID唯一值的DEPT_ID。
How can this be done? 如何才能做到这一点?
I would use: 我会用:
select dept_id
from x
group by dept_id
having min(dept_sub_id) <> max(dept_sub_id);
In many cases, two simple aggregations (such as min()
or max()
) have better performance than a count(distinct)
. 在许多情况下,两个简单的聚合(例如min()
或max()
)比count(distinct)
具有更好的性能。
Simply use count with distinct. 只需简单地使用count即可。
select dept_id,count(distinct dept_sub_id)
from dept
group by dept_id
having count(distinct dept_sub_id) > 1;
select dept_id, count(distinct dept_sub_id)
from x
group by dept_id
having count(distinct dept_sub_id) > 1
The keys here are count(distinct) which counts the distinctly different dept_sub_id's within the dept_id 这里的键是count(distinct),它计算dept_id中明显不同的dept_sub_id
Group by, obviously groups the counts by dept_id 分组依据,显然是按dept_id分组计数
having is like a "where" ran after the group-by does its grouping. 分组后再进行分组,就像“跑到哪里”一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.