Skip to content
Advertisement

SQL – Select the most close values to date in a table

Hi everyone i have a table called measure made up like this

And I want to select all the lowest values such as

I can do it by code but i prefer to use SQL for performance reason so I tried this query:

but the result is not good, I see too many results and are also somehow duplcated, how can i do?

EDIT Thank you so mutch for the help, the final ugly query is

so that I can have a table like that

If you want feel free to optimize my query

Advertisement

Answer

You can use the ROW_NUMBER function as a window function with OVER. The number the rows in the partition and sort by date

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