mysqlcumulative-sumcumulative-frequency

Add Mysql cumulative frequency field using any method


i have a sales 'table' and from it i have created a 'view' called dailyactivity which gives me the total sales made on each day. i am trying to add a cumulative frequency field but it returns an empty coloumn.can any one point me in the right direction.This is what i Have enter image description here

This is what i would like enter image description here


Solution

  • Query:

    SELECT
    d1.Date_TR,
    sum(d1.Sales) AS Daily_Sales,
    (SELECT SUM(d2.Sales)
     FROM dailyactivity d2
     WHERE d2.Date_TR <= d1.Date_TR) AS cumulative_sum
    FROM dailyactivity d1
    GROUP BY d1.Date_TR
    ORDER BY d1.Date_TR ASC