Problems wording a GROUP BY ranked query

From: "Ila B(dot)" <ilaria(dot)battiston(at)gmail(dot)com>
To: pgsql-novice(at)lists(dot)postgresql(dot)org
Subject: Problems wording a GROUP BY ranked query
Date: 2019-03-17 17:05:56
Message-ID: 5C9EA2E4-4338-4E8D-AD3C-6356CDADE366@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hello,

I have a table which looks like this:
Username | prescription_code | birth_date | prescription_date

I have 5 age classifications: 0-14, 15-24, 25-44, 45-64 and 65+.
I want to extract for each year (using date_part) the three most common prescriptions (maximum count) for each age classification (using just the date subtraction and converting years in days).

I would like results to be in the same table, to export it as CSV in an easy way using the IDE (PGAdmin 4, version 3.5 on Windows). I thought about using UNION and CTEs to extract rank, but that would mean writing 5 queries with CTEs. Is there a better way?

Thanks,
Ilaria

Browse pgsql-novice by date

  From Date Subject
Next Message Gavin M2301 2019-03-18 00:22:20 error installing on mingw
Previous Message Andreas Kretschmer 2019-03-14 09:28:38 Re: Elegant way to insert of some value in some tables in short instruction