我需要得到为每个日期分组的计数总和。现在我运行以下查询并按如下方式获取输出:
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.如何在查询中执行此操作?
答案 0 :(得分: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
注意:未经测试让我知道它是否不起作用
答案 1 :(得分: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' )
答案 2 :(得分:0)
尝试:
SELECT
timestamp t ,
(select count(*) from A where timestamp <= t)
FROM A
GROUP BY timestamp
ORDER BY timestamp
答案 3 :(得分:0)
这是一个简单的方法
SELECT
`timestamp` ,
COUNT( * )
FROM `A`
WHERE `timestamp` >= '2013-01-04 07:12:12'
GROUP BY DATE(`timestamp`)