SQL分组,然后在一个查询中取消分组

SQL分组,然后在一个查询中取消分组,sql,oracle,group-by,Sql,Oracle,Group By,嘿,我正在尝试做一个子查询,它允许我通过分组来获得一些东西的计数,然后基本上取消分组,并根据我刚刚得到的计数选择一定数量的行。如果SQL允许像contains方法之类的东西,那么这个查询基本上就是我想要的 select cat, mouse from pets where cat = (select cat from pets group by cat having count(mouse) > 3); 有什么

嘿,我正在尝试做一个子查询,它允许我通过分组来获得一些东西的计数,然后基本上取消分组,并根据我刚刚得到的计数选择一定数量的行。如果SQL允许像contains方法之类的东西,那么这个查询基本上就是我想要的

select cat, mouse
from pets
where cat = (select cat
             from pets
             group by cat
             having count(mouse) > 3);
有什么想法吗?使用oracle10g

select cat, mouse
from pets
where cat in (select cat
         from pet
         group by cat
         having count(mouse) > 3);
将“=”更改为“in” in关键字就是它的用途

将“=”更改为“in”
in关键字就是它的用途。

并不是说使用in子查询是非常错误的,而是使用分析函数的方法通常对这种自连接查询的性能要好得多


并不是说使用in子查询是非常错误的,而是使用分析函数的方法通常对这种自连接查询的性能要好得多


是的,不得不等待时间来这么做。
select cat, mouse from
(
    select cat, mouse, count(mouse) over(partition by cat) as ct
) 
where ct = 3