I have the following table
Date SoldAmt promo code article------------------------------------23/06/2014301120365930/06/2014711120365907/07/2014601120365914/07/2014211120365921/07/2014501120365928/07/20142111120365904/08/2014311120365911/08/2014911120365918/08/2014701120365925/08/2014401120365901/09/2014201120365908/09/2014401120365915/09/2014101120365922/09/2014311120365929/09/2014611120365906/10/2014311120365913/10/20144111203659I try to calculate average sum (SoldAmt)/number od days between the min date and max date rolling back the first 28 days/times in which promo =1 by Article
select sum(SoldAmt)/convert(day, Date)from MyTablegroupby[code article ],Date