Reputation: 1
I am new using SQL and I need to compare current year sales with the same weekday last year.
In the image below for example 06/01/2001 was on a Tuesday so I need to retrieve the same weekday last year with was on 05/01/2020 "Tuesday"
Thanks in advance
Upvotes: 0
Views: 817
Reputation: 1269873
If you have data every day, then you can use lag()
. The same weekday last year would be 52 weeks ago (at least that is a reasonable definition). So, using lag()
:
select t.*, lag(total_sales, 52*7) over (order by current_year)
from t;
Upvotes: 1