How to select the highest value after a count() | Sql Oracle

时间:2016-10-20 19:13:02

标签: sql oracle count max

This is my query:

SELECT f.name, COUNT(*) as num_books
from author f
JOIN book b on b.tittle = f.book
Group by f.name

Which gives me this table:

NAME                                                NUM_BOOKS
-------------------------------------------------- ----------
Dyremann                                                    2
Nam mann                                                    1
Thomas                                                      1
Asgeir                                                      1
Tullemann                                                   5
Plantemann                                                  1
Beste forfatter                                             1
Fagmann                                                     5
Lars                                                        1
Hans                                                        1
Svein Arne                                                  1

How could I easly alter the query to only display the author with the highest amount of released books? (While keeping in mind I'm rather new to sql)

4 个答案:

答案 0 :(得分:3)

Oracle, and as far as I know - only Oracle, allows you to nest two aggregate functions.

SELECT max (f.name) keep (dense_rank last order by count (*)) as name
from author f
JOIN book b on b.tittle = f.book
Group by f.name

In order to get ALL top authors:

select   name
from    (SELECT f.name,rank () over (order by count(*) desc) as rnk
         from author f
         JOIN book b on b.tittle = f.book
         Group by f.name
         ) 
 where   rnk = 1

Since Oracle 12c:

SELECT f.name
from author f
JOIN book b on b.tittle = f.book
Group by f.name
order by count (*) desc
fetch first row /* with ties (optional, in order to get all top authors) */

答案 1 :(得分:1)

The best way to do is to use:

SELECT f.name, COUNT(*) as num_books
from author f
JOIN book b on b.tittle = f.book
Group by f.name
Order by num_books DESC
FETCH FIRST ROW ONLY

This will order the results from biggest to smallest and return the first result.

答案 2 :(得分:1)

1) Oracle Specific : ( Using ROWNUM, For Postgres/MySql use limit )

select * from 
(SELECT f.name, COUNT(*) as num_books
from author f
JOIN book b on b.tittle = f.book
Group by f.name order by num_books desc )
 where ROWNUM = 1

2) General Query for all databases :

select f.name,count(*) as max_num_books from author f
JOIN book b on b.tittle = f.book
Group by f.name
having count(*) = 
(select max(num_books) 
from 
(SELECT f.name, COUNT(*) as num_books
from author f
JOIN book b on b.tittle = f.book
Group by f.name)
);

答案 3 :(得分:0)

我不确定为什么你首先需要加入。看来author表格中有一列book - 为什么仅count(book)来自该表,按name进行分组?这种安排非常奇怪 - author表应该只有作者属性,作者名称应该在title表中,但你加入author.book = book.title这似乎表明你做事实上,有这种奇怪的安排(因此你不需要加入)。此外,让表格和列(在另一个表格中)共享相同的名称book,是最好避免的做法。

在这种情况下,最基本的解决方案(不是最有效的解决方案)是

select name, count(book) as max_num_books
from   author
group by name
having count(book) = (select max(count(book) from author group by name);

子查询按名称分组,然后选择所有组计数的最大值。外部查询选择具有等于此最大值的帐簿计数的名称。子查询在单个列中返回单个行 - 单个值。这样的查询被称为"标量"子查询,可以在需要单个值的任何地方使用,例如外部查询的HAVING子句。 (它在HAVING子句中而不是WHERE子句,因为它引用了组属性 - count(book) - 而不是单个行属性。)

更有效的解决方案就像Dudu所示:

select name, ct as max_num_books
from   ( select name, count(*) as ct, rank() over (order by count(*) desc) rnk
         from   author
         group by name
       )
where rnk = 1;