0

I have a table that look like this:

S_ID   |   DATE
1        2016-01-01 
1        2016-01-02
1        2016-01-02
1        2016-01-05
1        2016-01-05
2        2017-01-02
2        2017-01-04
2        2017-01-04
2        2017-01-04
2        2017-01-04
2        2017-01-05

I am trying, in a single query to have the result below (adding cumulatively the records in time)

DATE        |  S_ID 1 |  S_ID 2 
2016-01-01  |    1    |   0
2016-01-02  |    3    |   1
2016-01-03  |    3    |   1
2016-01-04  |    3    |   5
2016-01-05  |    5    |   6

Any suggestions?

2
  • 1
    Use a "normalized" Output. Turning S_ID (row) into columns is bad, if you don't know every ID right away. Use a Group by S_ID,DATE to get a "usable result" per user and day.
    – dognose
    Feb 3, 2017 at 15:01
  • oh, "cumulative" - that adds a little overhead to a normalized query :-)
    – dognose
    Feb 3, 2017 at 15:21

3 Answers 3

1

Try this using group on date and conditional sum along with user variables to get cumulative sum.

Select date, 
    @s1 := @s1 + s_id_1 s_id_1,
    @s2 := @s2 + s_id_2 s_id_2
From (select 
    date, 
    sum(s_id = 1) s_id_1,
    sum(s_id = 2) s_id_2
from your_table
group by date
Order by date) t cross join (select @s1 := 0, @s2 :=0 ) t2;

It uses the fact that the true is 1 and false is 0 in mysql

2
  • Can you do this for 10.000 users?
    – dognose
    Feb 3, 2017 at 15:02
  • @dognose - Obviously, you don't want to do this in that case. Feb 3, 2017 at 15:44
0

You should better use

select 
    t.s_ID, 
    t.`date`,
    (SELECT SUM(1) FROM table x WHERE x.`date` <= t.`date` AND x.S_ID = t.S_ID) AS cumulative_sum
from table t
group by s_ID,`date`;

This will not provide your desired result, but a form of the result which works independent of the number of users, while the information stays the same:

s_ID | DATE      | cumulative_sum
1      2016-01-01  1 
1      2016-01-02  3
1      2016-01-05  5
2      2016-01-02  1
2      2016-01-04  5
2      2016-01-05  6

(If there's no entry for an id/date pair, the count didn't change that day)

0

You can arrived this with a PREPARED Statement. The first Query will generate a query with all s_id in your table. You only must change YOYOURTABLE to your table name : see the sample

    CONCAT('SELECT `date`,'
            ,GROUP_CONCAT(f1)
            ,' FROM YOURTABLE GROUP BY DATE')
    INTO @myquery
    FROM (
    SELECT DISTINCT CONCAT('sum(s_id = ',s_id,') AS sid_',s_id) AS f1
    FROM yourtable
    ) tab1;

-- ONLY for Test to verify the Query
SELECT @myquery;


PREPARE test FROM @myquery;
EXECUTE test;
DEALLOCATE PREPARE test;

sample

mysql> SELECT * FROM yourtable;
+------+------------+
| s_id | date       |
+------+------------+
|    1 | 2016-01-01 |
|    1 | 2017-02-02 |
|    2 | 2017-01-05 |
|    4 | 2016-03-04 |
|    7 | 2016-12-12 |
+------+------------+
5 rows in set (0,01 sec)

mysql> SELECT
    ->     CONCAT('SELECT `date`,'
    ->             ,GROUP_CONCAT(f1)
    ->             ,' FROM YOURTABLE GROUP BY DATE')
    ->     INTO @myquery
    ->     FROM (
    ->     SELECT DISTINCT CONCAT('sum(s_id = ',s_id,') AS sid_',s_id) AS f1
    ->     FROM yourtable
    ->     ) tab1;
Query OK, 1 row affected (0,01 sec)

mysql> SELECT @myquery;
+----------------------------------------------------------------------------------------------------------------------------------------+
| @myquery                                                                                                                               |
+----------------------------------------------------------------------------------------------------------------------------------------+
| SELECT `date`,sum(s_id = 1) AS sid_1,sum(s_id = 2) AS sid_2,sum(s_id = 4) AS sid_4,sum(s_id = 7) AS sid_7 FROM YOURTABLE GROUP BY DATE |
+----------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0,00 sec)

mysql> PREPARE test FROM @myquery;
Query OK, 0 rows affected (0,00 sec)

mysql> EXECUTE test;
+------------+-------+-------+-------+-------+
| date       | sid_1 | sid_2 | sid_4 | sid_7 |
+------------+-------+-------+-------+-------+
| 2016-01-01 |     1 |     0 |     0 |     0 |
| 2016-03-04 |     0 |     0 |     1 |     0 |
| 2016-12-12 |     0 |     0 |     0 |     1 |
| 2017-01-05 |     0 |     1 |     0 |     0 |
| 2017-02-02 |     1 |     0 |     0 |     0 |
+------------+-------+-------+-------+-------+
5 rows in set (0,00 sec)

mysql> DEALLOCATE PREPARE test;
Query OK, 0 rows affected (0,00 sec)

mysql>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.