Skip to content
Advertisement

Max date over each item

I have a table that simplified looks like this:

I want to query the latest cost of each item.

The way I do so is with a CTE getting the latest date of each item and then joining the cost.

My question is if there’s a simpler way to do it in one query without the CTE.

I tried to combine the MAX with the GROUP BY but didn’t found any way to make it work.

It only groups the records with the same item and cost when what I actually need is to group it accordingly with the MAX day.

Advertisement

Answer

The simplest is min/max with first/last option:

demo

Link to documentation and example.

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