Skip to content
Advertisement

How to make POSTGRESQL query based on first 2 parameters and latest third parameter

I have DB:

I need to make query request for last year(or last 12 months) group by Client_id, Version and latest date(!) for them. For example:

This is what I have right now:

And I’m getting result for EVERY DAY. If I’m removing DATE from group by, its complaining that Date should be in Group by.

I hope I did describe everything properly. I’m new here, so please let me know if I provide not full info.

Thank you for your time.

Advertisement

Answer

To get the most recent full year (i.e. not including the partial current day),

To consider today as part of the 12 month period, then:

So, assuming that you want the former option (you can switch it if you like):

p.s. recommend you don’t use “Date” as a column name since it’s a reserved word, and avoid using upper case letters in column names 🙂

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