3

I am Trying to Create cumulative column but unable to create it, please have look what i tried to calculate it.

mytable

create table mytable (
    TotalQuantity Decimal(7)
); 
insert into mytable (TotalQuantity) values
(0.0),
(0.0),
(1.0),
(0.0),
(2.0),
(0.0),
(0.0),
(0.0),
(0.0),
(0.0),
(0.0),
(0.0),
(0.0),
(0.0),
(0.0),
(0.0),
(0.0),
(0.0),
(0.0),
(0.0),
(0.0),
(0.0),
(0.0),
(1.0),
(1.0),
(0.0),
(1.0);

enter image description here

Based on above data set i wanted to calculate distinct wise count and cumulative sum

enter image description here

SELECT TotalQuantity AS DistinctTotalQuantity,
       COUNT(TotalQuantity) AS COUNTVALUE, 
       @running_total := @running_total + COUNT(TotalQuantity) AS cumulative_sum
FROM mytable 
JOIN (SELECT @running_total := 0) r
GROUP BY TotalQuantity
Strawberry
  • 33,750
  • 13
  • 40
  • 57
Juned Ansari
  • 5,035
  • 7
  • 56
  • 89

3 Answers3

3

The perhaps canonical ANSI standard way of doing a cumulative sum is via a correlated subquery. I make use of a view here which contains your base query:

CREATE VIEW test AS
SELECT
    TotalQuantity AS DistinctTotalQuantity,
    COUNT(TotalQuantity) AS COUNTVALUE, 
FROM mytable 
WHERE StoreId = 210 AND ProdName = 'Tusq'
GROUP BY TotalQuantity

Then compute the running total via:

SELECT
    t1.DistinctTotalQuantity,
    t1.COUNTVALUE,
    (SELECT SUM(t2.COUNTVALUE) FROM test t2
     WHERE t2.DistinctTotalQuantity <= t1.DistinctTotalQuantity) AS cumulative_sum
FROM test t1;

Using a session variable we could try:

SET @total = 0;
SELECT
    DistinctTotalQuantity,
    COUNTVALUE,
    (@total := @total + COUNTVALUE) AS cumulative_sum
FROM test;
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • it looks too expensive query, can't we do same using variable? – Juned Ansari Oct 14 '17 at 09:24
  • @JunedAnsari You could use a session variable to compute the rolling sum if you want. But part of the performance problem here is that you need to aggregate in one query and then take a rolling sum. This isn't the most performant situation. – Tim Biegeleisen Oct 14 '17 at 09:31
1

Following query will work perfectly and doesn't require too much changes in your query/approach. Although, I am not so sure about the performance:

SELECT TotalQuantity AS DistinctTotalQuantity,
       COUNT(TotalQuantity) AS COUNTVALUE, 
       (select @running_total := @running_total + count(TotalQuantity) from mytable m 
     where m.TotalQuantity = m1.TotalQuantity) cumulative_sum
    FROM (select *from mytable order by TotalQuantity) m1
    JOIN (SELECT @running_total := 0) r
    GROUP BY m1.TotalQuantity;

If you want to use floating values then declare column as follows:

create table `mytable` (
    `TotalQuantity` Decimal (7,1)  //Here 1 is scale. It means 1 digit is allowed after decimal point.
)

Here is my updated Sqlfiddle

Hope it helps!

Harshil Doshi
  • 3,497
  • 3
  • 14
  • 37
0
set @csum := 0;
update YourTable
set cumulative_sum = (@csum := @csum + count)
order by id;


set @csum := 0;
select id, count, (@csum := @csum + count) as cumulative_sum
from mytable
order by id;
Ravi Chauhan
  • 1,409
  • 13
  • 26