简体   繁体   English

从当前月份开始获取 mySQL 中按月计算的记录数

[英]Get month-wise count of records in mySQL starting with the current month

Below is my mysql table:下面是我的 mysql 表:

mysql> DESCRIBE mytable;
+---------------+--------------+------+-----+---------+-------+
| Field         | Type         | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| url_timestamp | timestamp    | NO   |     | NULL    |       |
| cr_num        | varchar(50)  | NO   |     | NULL    |       |
| status        | varchar(255) | NO   |     | NULL    |       |
| rollback_date | timestamp    | YES  |     | NULL    |       |
| rollback_user | varchar(255) | YES  |     | NULL    |       |
+---------------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

I wish to get the month-wise count for the cr_num which is primary key for the past 12 months.我希望获得过去 12 个月的主键 cr_num 的按月计数。

mysql> SELECT MONTHNAME(url_timestamp) , COUNT(url_timestamp) FROM mytable WHERE status='PRODUCTION' and url_timestamp >= NOW() - INTERVAL 1 YEAR GROUP BY MONTHNAME(url_timestamp);
+--------------------------+----------------------+
| MONTHNAME(url_timestamp) | COUNT(url_timestamp) |
+--------------------------+----------------------+
| November                 |                   43 |
| December                 |                   69 |
| January                  |                  220 |
| October                  |                  225 |
| February                 |                  209 |
| March                    |                  123 |
| April                    |                   93 |
| May                      |                  113 |
| June                     |                  217 |
| July                     |                  129 |
| August                   |                  185 |
| September                |                  415 |
+--------------------------+----------------------+
12 rows in set (0.01 sec)

There are three issues with the output i get.我得到的输出有三个问题。

  1. I want the output sorted starting with the current month listed first ie October 2020 all the way back to November 2019.我希望输出从当前列出的月份开始排序,即从 2020 年 10 月一直回到 2019 年 11 月。

  2. Months November and December are from previous year ie 2019; 11 月和 12 月来自上一年,即 2019 年; so i would like to display the year next to the each month.所以我想在每个月旁边显示年份。

  3. I would like to get the data only for this Year 2020 thus, November and December records should not show.我只想获取 2020 年的数据,因此不应显示 11 月和 12 月的记录。

I'm not from the database background, so I did not deep dive too much into what I could have tried.我不是来自数据库背景,所以我没有深入研究我可以尝试的内容。

Your conditions (2) and (3) are incompatible.您的条件(2)和(3)不相容。 To get months only from this year:仅从今年开始获取月份:

SELECT MONTHNAME(url_timestamp), COUNT(*)
FROM mytable
WHERE status = 'PRODUCTION' AND
      YEAR(url_timestamp) = YEAR(NOW())
GROUP BY MONTHNAME(url_timestamp)
ORDER BY MIN(url_timestamp)

You can use functions LAST_DAY() to group by month and DATE_FORMAT() to format the month with the year:您可以使用函数LAST_DAY()按月分组,使用DATE_FORMAT()将月格式化为年份:

SELECT DATE_FORMAT(LAST_DAY(url_timestamp), '%M %Y') month,
       COUNT(url_timestamp) counter
FROM mytable 
WHERE status='PRODUCTION' and url_timestamp >= NOW() - INTERVAL 1 YEAR 
GROUP BY month;

To get the rows sorted correctly you need to also GROUP BY LAST_DAY(url_timestamp) so you can use it in the ORDER BY clause:要正确排序行,您还需要GROUP BY LAST_DAY(url_timestamp)以便您可以在ORDER BY子句中使用它:

SELECT DATE_FORMAT(LAST_DAY(url_timestamp), '%M %Y') month,
       COUNT(url_timestamp) counter
FROM mytable 
WHERE status='PRODUCTION' and url_timestamp >= NOW() - INTERVAL 1 YEAR 
GROUP BY month, LAST_DAY(url_timestamp)
ORDER BY LAST_DAY(url_timestamp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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