Skip to content
Advertisement

List values with MaxDate

Im trying to create ie query to show itens with MAX DATE, but I don´t know how ! Follow the script and result:

I need to show only tasks with the last date of each one. Can you help me ?

Advertisement

Answer

You seem to be using Postgres (as suggested by the use of casting operator ::). If so – and I follow you correctly – you can use distinct on:

This guarantees one row per task only; which row is picked is controlled by the order by clause, so the above gets the row with the greatest date (time portion left apart). If there are ties, it is undefined which row is returned. You might want to adapt the order by clause to your exact requirement, if it is different than what I understood.


On the other hand, if you want top ties, then use window functions:

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