简体   繁体   中英

MySQL number of rows per time

I'm trying to build a query that shows the total number of users for every month/year. The query I have right now shows me the users created in any particular month/year point. But what I want is the sum of users till that point, not just the ones created in that period of time. This is the table structure:

mysql> desc users;
+-------------------+------------------+------+-----+---------+----------------+
| Field             | Type             | Null | Key | Default | Extra          |
+-------------------+------------------+------+-----+---------+----------------+
| id                | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| username          | int(11)          | NO   |     | NULL    |                |
| password          | varchar(255)     | NO   |     | NULL    |                |
| email             | varchar(255)     | NO   |     | NULL    |                |
| legajo            | int(10) unsigned | NO   |     | NULL    |                |
| doc_tipo          | varchar(45)      | NO   |     | NULL    |                |
| doc_nro           | int(10) unsigned | NO   |     | NULL    |                |
| activo            | int(10) unsigned | NO   |     | 0       |                |
| token_actv        | varchar(255)     | NO   |     | NULL    |                |
| token_fecha_envio | datetime         | NO   |     | NULL    |                |
| created           | datetime         | YES  |     | NULL    |                |
| modified          | datetime         | YES  |     | NULL    |                |
| role              | varchar(60)      | NO   |     | usuario |                |
| nombres           | varchar(255)     | YES  |     | NULL    |                |
| apellido          | varchar(255)     | YES  |     | NULL    |                |
+-------------------+------------------+------+-----+---------+----------------+

This is the query.

SELECT CONCAT_WS('/',Month(created),YEAR(created)), Count(*)
FROM users
GROUP BY Month(created),YEAR(created)
ORDER BY Month(created) ASC ,YEAR(created) ASC

Results from current query looks like:

mysql> SELECT CONCAT_WS('/',Month(created),YEAR(created)), Count(*) FROM users GROUP BY Month(created),YEAR(created) ORDER BY Month(created) ASC ,YEAR(created) ASC LIMIT 5;
+---------------------------------------------+----------+
| CONCAT_WS('/',Month(created),YEAR(created)) | Count(*) |
+---------------------------------------------+----------+
| 1/2010                                      |       79 |
| 1/2011                                      |       70 |
| 1/2012                                      |       70 |
| 1/2013                                      |       80 |
| 1/2014                                      |       64 |
+---------------------------------------------+----------+

Any help will be greatly appreciated.

All you need to do is use the query you have, as the input to another query, and then use a mysql variable to hold the cumulative sum - lets say you alias your year/month string as d, and your count value as c:

select d , @sum := @sum + c as rolling_total from (
  SELECT CONCAT_WS('/',Month(created),YEAR(created)) d, Count(*) c
    FROM users
    GROUP BY Month(created),YEAR(created)
    ORDER BY Month(created) ASC ,YEAR(created) ASC
  ) q cross join (select @sum := 0) qq

demo here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM