I am not very familiar with sql however I have to do a query for a sale prediction.
The data is for the sale with distinct prodID, shopID, weekDay, date and Sale. I need to get the sale of the same product in the same shop and same weekDay in the past (e.g) 3 weeks. Therefore some proper pivoting is necessary. There might be that for some days there is no sale record for that product-shop-weekday combination. Also importantly I have to ignore negative sale values (if any) for average calculation.
This is header of the data:
DATE prodID shopID sale weekDay
2017-03-01 8 16 4.8 Wednesday
2017-03-01 2 16 18.8 Wednesday
2017-03-01 62 16 1.7 Wednesday
2017-03-01 34 16 3.6 Wednesday
2017-03-01 32 16 12.0 Wednesday
2017-03-02 8 16 3.6 Thursday
2017-03-02 34 16 15.8 Thursday
Ideal outcome is:
DATE prodID shopID sale weekDay saleWeek-1 saleWeek-2 saleWeek-3 ave_3sale
Perhaps a self-join can be used to build the new columns. Thank you very much much for your help.