Skip to content
Advertisement

SQL order with equal group size

I have a table with columns month, name and transaction_id. I would like to count the number of transactions per month and name. However, for each month I want to have the top N names with the highest transaction counts.

The following query groups by month and name. However the LIMIT is applied to the complete result and not per month:

Does anyone have an idea how I can get the top N results per month?

Advertisement

Answer

Use row_number():

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement