Search code examples
postgresqlaggregate-functions

How to calculate average number and give subquery label


I have two table "book" and "authorCollection". Because a book may have multi-authors, I hope to get the average number of authors in table "book" which published after year 2000(inclusive). For example:

Table Book:
key  year
1     2000
2     2001
3     2002
4     1999

Table authorCollection:
key author
1    Tom
1    John
1    Alex
1    Mary
2    Alex
3    Tony
4    Mary

The result should be (4 + 1 + 1) / 3 = 2;(key 4 publish before year 2000). I write the following query statement, but not right, I need to get the number of result in subquery, but cannot give it a label "b", How can i solve this problem? And get the average number of author? I still confused about "COUNT(*) as count" meaning....Thanks.

SELECT  COUNT(*) as count,  b.COUNT(*) AS total 
FROM A
WHERE key IN  (SELECT key
               FROM Book
               WHERE year >= 2000
               ) b
GROUP BY key;

Solution

  • First, count number of authors for a key in a subquery. Next, aggregate needed values:

    select avg(coalesce(ct, 0))
    from book b
    left join (
        select key, count(*) ct
        from authorcollection
        group by 1
        ) a
    using (key)
    where year >= 2000;