Skip to content
Advertisement

SQL window function with date filter

Looking to do a LAG/LAST_VALUE/MAGIC window function on a table which somehow needs to include a date filter (as in how far back it should go). I’m trying to get the value as of 7 days ago onto every row. Not sure this can be achieved with a fancy window function in a sparse data set. As the data is quite large, it would need to be an efficient query query as well.

Test Query (wrong result):

Output:

Advertisement

Answer

I think you want apply:

In other databases, you could use a fancy window frame with range between. However, SQL Server does not support such functionality.

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