Skip to content
Advertisement

How to get the last amount paid grouped by city and year?

I would like to know how to get the amounts paid for the last two months paid per city ordered per year.

If I group by year, city name and two months :

I would have something like this :

But the output I want is something like this:

Does anyone know how I can do this?

Advertisement

Answer

You may use a subquery to find the most values of twoMonths for each year and city, and the join to it:

If your version of SQL support ROW_NUMBER, then it can provide an alternative:

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