Aline Dennis
Aline Dennis

Reputation: 1

SQL query to find the same week da last year

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

Data with columns

Upvotes: 0

Views: 817

Answers (1)

Gordon Linoff
Gordon Linoff

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

Related Questions