Reputation: 31
I don't know anything about SQL. I currently have a query that gives me this. Sales for some products by channel/etc (please note this is a very simplified version, there's more fields) by week/period/year:
Basically what I would need is to add a column that gives me the sales for prior year. Basically, transform the table as below. In Excel it would be a simple sumifs that would just sum the same exact criteria aside from the year which would be the previous year.
Is it possible to do this within SQL? The dataset is too large to do it within Excel.
Upvotes: 3
Views: 1834
Reputation: 1269693
I think you just want lag()
:
select t.*,
lag(sales) over (partition by channel, product, weekno order by yearno) as prev_sales
from t;
If I understand the data, then periodno
is redundant with weekno
.
Upvotes: 1