SQL 最小和计数

SQL Min and Count

我想要实现的是显示类别中的图书数量

其中 Count(Category) > Count(Category) 中的最小数量

示例;

如果类别是

A = 1
b = 2
c = 3
D = 1
E = 1

我正在尝试使用 MIN 显示大于 1 的类别。

我得到的错误是:

ORA-00935: group function is nested too deeply

SELECT Count(Category),
       Category 
From Books
Having Count((Category) > MIN(Count(Category)
Group BY Category  

正在寻找这样的东西:

Select Count(Category),
       Category 
From Books 
Group BY Category 
Having Count(Category) > (Select Min(cnt)
                          from (Select Count(Category) AS cnt
                                From Books
                                Group By Category))

这将 select 所有类别的计数大于所有类别中的最小计数。

应该这样做:

 SELECT Category, CategoryCount from
 (SELECT rownum as r, Category, Count(*) as CategoryCount
 From Books 
 Group BY Category
 Order by CategoryCount asc)
 Where r > 1;

另一种方法是 rank 从最低开始计数(关系被分配相同的排名)并且仅 select 排名大于 1 的行:

select * from (
    select count(*) cnt, category,
      rank() over (order by count(*)) rn
    from books
    group by category  
) t where rn > 1

Giorgos 的回答是正确的。它可以使用子查询因子重新排列(并稍微提高效率):

with ctg (category, categ_count) as (
        select   category, count(*)
        from     books
        group by category
     )
select category, categ_count
from   ctg
where  categ_count > (select min(categ_count) from ctg);