Re: SUM and MAX over different periods - months over several years?

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Paul Linehan <linehanp(at)tcd(dot)ie>
Cc: pgsql-novice <pgsql-novice(at)postgresql(dot)org>
Subject: Re: SUM and MAX over different periods - months over several years?
Date: 2018-04-30 14:36:09
Message-ID: CAKFQuwYFLLRC_9Qo=doDKQwo0kZWWvHYvaKSrqdt9y32BgxwDg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Mon, Apr 30, 2018 at 7:25 AM, Paul Linehan <linehanp(at)tcd(dot)ie> wrote:

> Now, what I want is to find which illness was reported most in a given
> month and in a given year.
>

Basically, you want to computed an ordered listing of all illnesses counts
grouped by year-month, then return the first one of them (in the case of
ties are you expecting to return two records or one)?

SELECT DISTINCT ON (year, month) year, month, count_of_illness, illness
FROM (
SELECT year, month, count(*) AS count_of_illness, illness
FROM ...
GROUP BY year, month
ORDER BY 1, 2, 3 DESC
) grp

This will return exactly one record, "the first" for each year/month
combination in your data. First is determined by the sort in the subquery.

If you need to return multiple records in the case of ties you either, more
of less, self-join on (year, month, count) or use something like
dense_rank() OVER (partition by year, month order by count_of_illness desc)
to assign a rank of 1 to all highest count items and then add a "where
dense_rank = 1" filter to the query.

David J.

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Ron Watkins 2018-05-03 19:20:08 Postgres warm standby with delay
Previous Message Paul Linehan 2018-04-30 14:25:53 SUM and MAX over different periods - months over several years?