1

可能重复:
在 MySQL 中计算运行总计

我需要得到为每个日期分组的计数总和。现在我正在运行以下查询并得到如下输出:

SELECT  `timestamp` , COUNT( * ) 
FROM  `A` 
WHERE  `timestamp` >=  '2013-01-04 07:12:12'
GROUP BY DATE_FORMAT( `timestamp` ,  '%Y-%m-%d' ) 

我得到

输出:

timestamp                                               count(*)
-------------------------------------------------- -----------
2013-01-04 07:58:21                                4
2013-01-05 09:28:56                                38
2013-01-06 00:03:04                                10

现在我需要的是,我需要得到按日期分组的计数总和。那是第二个日期应该是 42,第三个日期应该是 52。如何在查询中执行此操作?

4

4 回答 4

0

尝试 :

SELECT 
   timestamp t , 
   (select count(*) from A where  timestamp <= t)
FROM  A
GROUP  BY timestamp
ORDER  BY timestamp
于 2013-01-31T10:11:19.730 回答
0

你可以试试下面的SQL

SELECT DATE_FORMAT( ts_date ,  '%Y-%m-%d' ) as ts_dt_out, SUM(cnt) 
FROM
(
SELECT  `timestamp` ts_date , COUNT( * )  as cnt
FROM  `A` 
WHERE  `timestamp` >=  '2013-01-04 07:12:12'
GROUP BY DATE_FORMAT( `timestamp` ,  '%Y-%m-%d' ) 
)
as inner
WHERE ts_date >=  '2013-01-04 07:12:12'
GROUP BY ts_dt_out

注意:未经测试,如果它不起作用,请告诉我

于 2013-01-31T10:04:33.943 回答
0

试试看:

SELECT  `timestamp` , @sum:= ifnull(@sum, 0 ) + COUNT( * ) 
FROM  `A` 
WHERE  `timestamp` >=  '2013-01-04 07:12:12'
GROUP BY DATE_FORMAT( `timestamp` ,  '%Y-%m-%d' ) 
于 2013-01-31T10:08:59.667 回答
0

这是一个简单的方法

SELECT  
    `timestamp` , 
    COUNT( * ) 
FROM  `A` 
WHERE  `timestamp` >=  '2013-01-04 07:12:12'
GROUP BY DATE(`timestamp`) 
于 2013-01-31T10:31:33.683 回答