[英]Find row that has same value in one column over multiple rows while another column has different values
[英]Query to return row values which has same values in all the rows for each different value of one column
例如,我有以下数据:
我只想显示第 2、3、5 行,因为 (01, 02, 03) 中的每个 A6 的所有 A1、A2、A3、A4 值都相同(每行必须具有每个 A6 值)。 所以我只想排除第 4 列。
我知道您想要值1
、 2
和3
都可用于给定(a1, a2, a3, a4)
元组的记录。
这是通过将表与聚合子查询连接来实现的一种方法:
select t.*
from mytable t
inner join (
select a1, a2, a3, a4
from mytable t1
where a6 in (1, 2, 3)
group by a1, a2, a3, a4
having count(distinct a6) = 3
) g
on g.a1 = t.a1
and g.a2 = t.a2
and g.a3 = t.a3
and g.a4 = t.a4
您还可以使用相关子查询进行过滤:
select t.*
from mytable t
where (
select count(distinct a6)
from mytable t1
where t1.a1 = t.a1 and t1.a2 = t.a2 and t1.a3 = t.a3 and and t1.a4 = t.a4
) = 3
如果你想要 A2/A3/A4 有重复的行,那么你可以使用窗口函数:
select t.*
from (select t.*,
count(distinct (case when a6 in (1, 2, 3) then a6 end) over (partition by a2, a3, a4) as cnt
from t
) t
where cnt > 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.