我在 mysql 中的聚合公式遇到了一些问题。我怀疑该错误是因为计算机在排序/排序之前进行了聚合。有人可以建议一种更好的方法来汇总计数值,或者帮助我识别代码中的错误吗?这是结果表
date hour count aggregate
August, 11 2012 20 1 170
August, 12 2012 19 1 58
August, 13 2012 14 1 44
August, 13 2012 15 1 75
August, 13 2012 21 1 72
August, 13 2012 23 1 80
该表首先按日期排序,如果两次在同一天,则按小时排序。由于聚合函数是基于计数函数的,而计数列只是用 1 填充,所以聚合列应该显示 1、2、3、4...等 这是我的查询
SELECT t1.original, t1.hour, t1.count, SUM(t2.count) as aggregate
FROM
(SELECT date(FROM_UNIXTIME( timecode)) as day,(hour(FROM_UNIXTIME( timecode))) AS hour,(FROM_UNIXTIME( timecode)) AS original, COUNT(count) as 'count'
FROM sampleData
GROUP BY day, hour
order by original
) t1
INNER JOIN
(SELECT date(FROM_UNIXTIME( timecode)) as day, (hour(FROM_UNIXTIME( timecode) )) AS hour,(FROM_UNIXTIME( timecode)) AS original, COUNT(count) as 'count'
FROM sampleData
GROUP BY day, hour
order by original
) t2
on t1.hour >= t2.hour
GROUP BY t1.hour
ORDER BY t1.original