我有一个名为“results”的表,其中包含许多行和一个名为“timestamp”的列。
我想对每个月的行数进行分组。但对于他们中的每一个,我也想考虑前几个月的行。
所以,如果我有
Jun/13
Jun/13
Jun/13
Jul/13
Jul/13
Jul/13
Jul/13
Jul/13
Aug/13
Aug/13
Aug/13
Aug/13
Aug/13
Aug/13
Aug/13
结果将是
3 Jun/13
8 Jul/13
15 Aug/13
这就是我现在所拥有的,但不考虑前几个月。
SELECT DATE_FORMAT(FROM_UNIXTIME(timestamp), '%e %b %Y'), COUNT(1) FROM results
GROUP BY DATE_FORMAT(FROM_UNIXTIME(timestamp), '%e %b %Y');
答案 0 :(得分:3)
我让这个工作:
SELECT month, count, @total:=@total+count AS total
FROM (SELECT @total:=0) t STRAIGHT_JOIN
(SELECT DATE_FORMAT(FROM_UNIXTIME(timestamp), '%e %b %Y') AS month, COUNT(*) AS count
FROM results GROUP BY EXTRACT(YEAR_MONTH FROM FROM_UNIXTIME(timestamp))) AS m;
+------------+-------+-------+
| month | count | total |
+------------+-------+-------+
| 1 Jun 2013 | 3 | 3 |
| 1 Jul 2013 | 5 | 8 |
| 1 Aug 2013 | 7 | 15 |
+------------+-------+-------+
答案 1 :(得分:1)
以下解决方案应该有效......
SELECT COUNT(1),DATE_FORMAT(timestamp,'%b /%y')FROM FROM GROUP BY YEAR(时间戳),MONTH(时间戳);
祝你好运Talki
答案 2 :(得分:0)
如果我理解正确,这应该有效;
SELECT COUNT(*) num, SUBSTR(ts, 1, 7) month
FROM results
JOIN (SELECT MAX(timestamp) ts FROM results
GROUP BY YEAR(timestamp),Month(timestamp)) mm
ON results.timestamp <= mm.ts
GROUP BY SUBSTR(ts, 1, 7);