Skip to content
Advertisement

how can i check if value exists before the date specified in sql server

I have the data below in a sql table,

let assume the user wants to get data of 2020-11-13 which is

but i’d like to get the previous supplier due as well before the date specified which is

along with

so the actual query i wanna get is this

and if there is no previous due i wanna return

i couldn’t even figure out how to write the query because i dont understand how to go about it

Advertisement

Answer

You can use window functions. Assuming that date can be used to consistently order the records of each supplier:

A typical alternative is a subquery, or a lateral join:

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