group by, order by and pick last value of a column in each group I am using PostgreSQL 9.6. I have one "events" table. I need to group by an "item_id" column then order by "date" column in each group and then pick last value of "value_num" column in each group. Pls, help me how this could be done.
I suppose you want a "greatest-n-per-group" query. The easiest (but not necessarily the most efficient) way to achieve this in Postgres is using DISTINCT ON:

select distinct on (item_id)        -- defines the "group"
    item_id, date, value_num        -- show 1 row per group
from 
    events
order by 
    item_id, date desc ;            -- which order to be used