I have a sales dataset where I need to calculate last 3 month sales average for each product entry.
This is what it is supposed to look like. So for a September entry for a given store, sales rep, product combination , L3M should be Sales of (June + July + August) / 3, if for a given rep there's no data entries for say July then it should just be (June + August) / 2.
The same sql query needs to calculate 'Sales' on a daily level, and have a window function that calculates last 3 month average for the corresponding sales entry. How can i achieve this?
Here's a sample table
CREATE TABLE SalesData (
the_date Date,
cal_month varchar(255),
region_code varchar(255),
store_id varchar(255),
sales_rep varchar(255),
product_id varchar(255),
sales_amt float
);
and some sample data
insert into SalesData (the_date, cal_month, region_code, store_id, sales_rep, product_id, sales_amt)
values ('2022-06-03', '202206', 'USNE', '4123', '65','1','50'),
('2022-06-19', '202206', 'USNE', '4123', '65','1','10'),
('2022-06-27', '202206', 'USNE', '4123', '65','2','60'),
('2022-07-02', '202207', 'USNE', '4123', '65','1','00'),
('2022-08-05', '202208', 'USNE', '4123', '65','1','30'),
('2022-08-09', '202208', 'USNE', '4123', '65','2','10'),
('2022-08-06', '202208', 'USNE', '5646', '32','1','100'),
('2022-08-06', '202208', 'USNE', '5646', '32','2','120'),
('2022-09-03', '202209', 'USNE', '4123', '65','1','70'),
('2022-09-07', '202209', 'USNE', '4123', '65','2','20'),
('2022-09-07', '202209', 'USNE', '5646', '32','2','30');
So, here for September entries the L3M column will be calculated like this:
For product 1 for sales rep 65, L3M should be (sum(june) + sum(july) + sum(august))/3 = (60 + 0 + 30) / 3 = 30
and for product 2 (sales rep 65), L3M should be (60 + 10) / 2 = 35
and for product 2 (sales rep 32), L3M should be 120 / 1 = 120