I have a table below and i want to aggregate the number for each day and product. So i want to get the aggregate each day by product, even when the product was not available on that day.
Source table
Date | product | Amount |
---|---|---|
06/01/2021 | a | 300 |
05/01/2021 | a | 200 |
04/01/2021 | a | -100 |
03/01/2021 | a | -500 |
02/01/2021 | a | 200 |
01/01/2021 | a | 100 |
05/01/2021 | b | 500 |
03/01/2021 | b | -900 |
02/01/2021 | b | 500 |
02/01/2021 | c | 250 |
Expected outcome |Report Date|product|Amount |-------------|-----------|----------- |06/01/2021|a|200 |06/01/2021|b|100 |06/01/2021|c|250 |05/01/2021|a|-100 |05/01/2021|b|100 |05/01/2021|c|250 |04/01/2021|a|-300 |04/01/2021|b|-400 |04/01/2021|c|250 |03/01/2021|a|-200 |03/01/2021|b|-400 |03/01/2021|c|250 |02/01/2021|a|300 |02/01/2021|b|500 |02/01/2021|c|250 |01/01/2021|a|100
Please let me know if this is possible
Script method: I added a return for product a on the 6th to show why I added the group by.
RAW:
load * inline [
Date, Product, Amount
06/01/2021, a, 300
06/01/2021, a, -100
05/01/2021, a, 200
04/01/2021, a, -100
03/01/2021, a, -500
02/01/2021, a, 200
01/01/2021, a, 100
05/01/2021, b, 500
03/01/2021, b, -900
02/01/2021, b, 500
02/01/2021, c, 250
];
SALES:
load distinct
Date
resident RAW;
left join
load Distinct
Product
Resident RAW;
left join
load
Date,
Product,
sum(Amount) as Amount
resident RAW
group by
Date,
Product; drop table RAW;
SALES_FIN:
NoConcatenate
load
Date,
Product,
if(isnull(Amount),0,Amount) as Amount
Resident SALES; drop table SALES;