MySQL计数记录每天累计

时间:2017-02-03 14:56:58

标签: mysql

我的桌子看起来像这样:

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

我正在尝试,在单个查询中得到以下结果(及时累计记录)

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

有什么建议吗?

3 个答案:

答案 0 :(得分:1)

尝试使用日期组和条件和以及用户变量来获得累积总和。

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;

它使用的事实是true为1,而mysql中的false为0

答案 1 :(得分:0)

你最好使用

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`;

这不会提供您想要的结果,但结果的形式与用户数量无关,而信息保持不变:

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

(如果没有id / date对的条目,那天的计数没有改变)

答案 2 :(得分:0)

您可以使用PREPARED语句到达此处。第一个Query将生成一个包含表中所有s_id的查询。您只需将YOYOURTABLE更改为您的表名: 看样品

    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;

<强>样品

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>