Postgres:在已分组的查询中对另一列的不同列进行平均

时间:2017-04-04 19:01:00

标签: postgresql aggregate

如果在不使用子查询的情况下将查询分组用于其他目的,是否有办法仅在另一列的不同列上对列进行平均?我知道它可以通过子查询完成,但试图避免重构旧查询,除非绝对必要。

现有查询虽然复杂,但与下面的示例大致相同。如您所见,图书馆有任意数量的书籍,书籍有任意数量的章节,章节有任意数量的段落,而查询则返回每个图书馆的书籍和段落总数。

SELECT libraries.name,
 COUNT(DISTINCT books.id) AS num_books,
 COUNT(paragraphs.id) AS num_paragraphs
FROM libraries
LEFT JOIN books ON books.library_id = libraries.id
LEFT JOIN chapters ON chapters.book_id = books.id
LEFT JOIN paragraphs ON paragraphs.chapter_id = chapters.id
GROUP BY libraries.name

现在假设表 books 有一个列 publish_year ,我想要发布库中的平均年鉴。显然,我不能简单地添加 AVERAGE(books.publish_year),因为有更多章节和段落的书籍会使平均值偏差。

是否有一种很好的方法可以在不重构查询的情况下再次基于不同的 books.id 平均 books.publish_year ,或者重新构建查询是不可避免的?

1 个答案:

答案 0 :(得分:0)

加入前的窗口功能

select
    l.name,
    count(distinct b.id) as num_books,
    count(p.id) as num_paragraphs,
    min(year_avg) as year_avg
from
    libraries l
    left join (
        select *, avg(publish_year) over(partition by library_id) as year_avg
        from books
    ) b on b.library_id = l.id
    left join chapters c on c.book_id = b.id
    left join paragraphs p on p.chapter_id = c.id
group by l.name