Skip to content
Advertisement

Postgres – Pick 1 max row instead of all max rows in the same month on a LOD (group by)

Hi have a data set which has product, site, station, date and some numerical fields such as sn_count, blob etc. Within every combination of product, site and station, if there are multiple entries for the same month from different dates, I want to only pick one row with max sn count in that month.

The code I have right now is working for the most part. It is filtering out rows with lesser sn counts in that month. But it gives me all rows with the same max sn count. Whereas, I just want 1 from a month.

This is my code:

This is the result I have: enter image description here

The highlighted rows have the same sn count and is the max sn count for that month. I however, only want one of these rows. Not both.

Advertisement

Answer

My guess is that you have two observations with the same dist_sn_count_at_blob.

This is a candidate for PostgreSQL’s distinct on.

Please try something like this:

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