Rolling Count Calculation

Hi, I am trying to get a rolling count of data over a week, 4 weeks, and 8 weeks period. This is the sql query I have in the transforms dataflow. Then in the analyzer, I would select Count of 'Rolling_Week', 'Rolling_4_Weeks', and 'Rolling_8_Weeks'. But I am getting 0 for everything, which is not accurate. Could anyone help me with this? Thank you! 

SELECT *, 
(SELECT `Dlvd_Dolls`
FROM `delivered_sales_location_data`
WHERE `Dlvd_Date` BETWEEN NOW() AND DATE_SUB(NOW(), INTERVAL 7 DAY)) AS Rolling_Week,
(SELECT `Dlvd_Dolls`
FROM `delivered_sales_location_data`
WHERE `Dlvd_Date` BETWEEN NOW() AND DATE_SUB(NOW(), INTERVAL 28 DAY)) AS Rolling_4_Weeks,
(SELECT `Dlvd_Dolls`
FROM `delivered_sales_location_data`
WHERE `Dlvd_Date` BETWEEN NOW() AND DATE_SUB(NOW(), INTERVAL 56 DAY)) AS Rolling_8_Weeks
FROM `delivered_sales_location_data`

Best Answer

  • MarkSnodgrass
    Answer ✓

    Your BETWEEN statement needs to have the older date first, followed by the newer date, It should look like this, for example: 

    WHERE `Dlvd_Date` BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW())  AS Rolling_Week

     

    **Check out my Domo Tips & Tricks Videos

    **Make sure to <3 any users posts that helped you.
    **Please mark as accepted the ones who solved your issue.

Answers

  • MarkSnodgrass
    Answer ✓

    Your BETWEEN statement needs to have the older date first, followed by the newer date, It should look like this, for example: 

    WHERE `Dlvd_Date` BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND NOW())  AS Rolling_Week

     

    **Check out my Domo Tips & Tricks Videos

    **Make sure to <3 any users posts that helped you.
    **Please mark as accepted the ones who solved your issue.
  • That makes sense! Thank you so much!