Create a Cumulative Sum Column in MySQL

前端 未结 9 1961
青春惊慌失措
青春惊慌失措 2020-11-22 00:05

I have a table that looks like this:

id   count
1    100
2    50
3    10

I want to add a new column called cumulative_sum, so the table wou

9条回答
  •  攒了一身酷
    2020-11-22 00:21

    Sample query

    SET @runtot:=0;
    SELECT
       q1.d,
       q1.c,
       (@runtot := @runtot + q1.c) AS rt
    FROM
       (SELECT
           DAYOFYEAR(date) AS d,
           COUNT(*) AS c
        FROM  orders
        WHERE  hasPaid > 0
        GROUP  BY d
        ORDER  BY d) AS q1
    

提交回复
热议问题