Click here to Skip to main content
15,889,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,
I can take date wise count using group by in mysql.But i want date wise cumulative count in mysql.How?


For example,
date count
10-11-2011 200
11-11-2011 500
12-11-2011 700


This is day wise count.

But i want day wise cumulative count.

date count
10-11-2011 200
11-11-2011 700
12-11-2011 1400


like this.
Posted
Updated 25-Nov-11 2:27am
v2

1 solution

Here's one possible solution. Create a function that takes a date as an input and calculates the cumulative count for that date and then returns the count. Then in your original query, replace the count(some_field) with a call to the function, passing the function the row's date.
create or replace function calc_cumulative_count(input_date DATE)
  return   LONG
as
  cumulative_count     LONG;
  select count(your_field_name)
    into cumulative_count
    from your_table
   where some_date_field <= input_date;

  return cumulative_count;
end calc_cumulative_count;
/

If your exisiting query is
select some_date_field
      ,count(your_field_name)
  from your_table
 group by some_date_field
;

this can be changed to
select distinct some_date_field
      ,calc_cumulative_count(some_date_field)
  from your_table
;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900