0

I have a simple group by query:

SELECT timestamp, COUNT(users)
FROM my_table
GROUP BY users

How do I add a sum_each_day column that will sum the users count of each row and will aggregate it forward to the next row and so on

The output should be like this:

timestamp | users | sum_each_day
2015-11-27  1          1
2015-11-28  5          6
2015-11-29  3          9
2015-11-30  7          16

Thanks in advance

6
  • Do you have timestamp or a date field? because I dontt see you agregated by timestamp Commented Nov 30, 2015 at 21:24
  • Paste database explain DDL Commented Nov 30, 2015 at 21:26
  • It's a date field and I am counting the users on each day Commented Nov 30, 2015 at 21:26
  • @RagenDazs what do you mean by DDL ? There are only two columns : timestamp and users I just want to count the users each day and aggregate the totals on the following day on Commented Nov 30, 2015 at 21:29
  • It is strange to see a group by on a column that is being aggregated in the select list, while the column that is not aggregated is not listed in the group by. Neither is allowed in standard sql. Makes it hard to understand, and I wonder if it is intended. Commented Nov 30, 2015 at 21:36

4 Answers 4

1

You could use a sub-query, like this:

SELECT    timestamp,
          num_users,
          (SELECT COUNT(users)
           FROM   my_table
           WHERE  timestamp <= main.timestamp) sum_users
FROM      (
          SELECT   timestamp,
                   COUNT(users) num_users
          FROM     my_table
          GROUP BY timestamp
          ) main
Sign up to request clarification or add additional context in comments.

5 Comments

Instead of timestamp.. you problably need something like from_unixtime(mytimestamp, '%Y %D %M')
OP want both column COUNT(users) and SUM(users)
@trincot I have tried it but the new select count user column bring me 0 value
@trincot BINGO ! this is it :) Thank you very much ! BTW, if I would like to add third column to this query in the future, let say the name is 'area' how should I integrate it here ?
Adding a column really depends on how you want to aggregate it, because you are now showing one result per timestamp, but at the base you have multiple records per timestamp. So if you have an extra field to show, you need to decide whether you want the sum of it, the maximum of it, ... etc. Assuming that area is numeric and you want the sum per timestamp, then add SUM(area) sum_area next to COUNT(users) num_users, and select sum_area at the top. The principle is similar with max.
0

If you really need this in mysql it'll cost some performance but i believe a sub query with a count will solve it:

SELECT t1.timestamp, count (), select count () from my_table t2 where t2.timestamp <= t1.timestamp From my_table t1 Group by users

If you display this data through a scripting language like PHP it would be easier to keep a counter and display the aggregate per row.

Comments

0

I would do this using variable:

SET @total := 0;
SELECT timestamp, DayCount, (@total := @total + DayCount) AS Total
FROM
    (SELECT timestamp, COUNT(users) AS DayCount
     FROM my_table
     GROUP BY timestamp) AS t1

Fiddler: I am not using your table structure here, but you can get idea

Comments

0

If I understand correclty, this will work:

set @c=0;
SELECT `timestamp`,sum(`users`),(select @c:=@c+sum(`users`))
FROM `my_table` 
group by `timestamp`;

4 Comments

you understand me right but the new sum of users column returns exactly the sam as the count column
@Abraham I've tested the query and it returns exatly same result as you've posted in output. Are you using InnoBD storage engine?
Ivichunk , yes I am using tInnoBD storage engine
@Abraham Then sorry, this example works fine for me and I don't know what can be the issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.