对于一列的每个不同值,返回在所有行中具有相同值的行值

问题描述 投票:0回答:1

例如,我有以下数据:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9Wem9JSS5qcGcifQ==” alt =“示例截图”>

我只想显示第二,第三,第五行,因为(01、02、03)中每个A6的所有A1,A2,A3,A4值都相同(每行必须具有每个A6值)。所以我只想排除第4列。

sql oracle oracle10g union
1个回答
0
投票

我了解您想要记录,对于给定的1元组,值23(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
© www.soinside.com 2019 - 2024. All rights reserved.