选择并创建月份行并计算数据 - MySQL

时间:2015-06-11 01:49:40

标签: mysql count

我遇到了问题。我有如下数据:

-----------------------
|  last_login_dt      |
-----------------------
| 2015-08-11 08:06:36 |
| 2015-06-10 22:06:43 |
| 2015-06-11 08:06:58 |
| 2015-09-11 08:06:45 |
-----------------------

到目前为止,我设法计算了这种数据并使用以下语句转换为此数据:

SELECT DATE_FORMAT(last_login_dt,'%m/%Y') as `month`,
count(last_login_dt) as `total_visits` from public_user
WHERE DATE_FORMAT(last_login_dt,'%Y') = YEAR(CURDATE()) 
group by DATE_FORMAT(last_login_dt,'%m/%Y')
order by `month` asc;

----------------------------
|  month  |  total_visits  |
----------------------------
| 06/2015 |       2        |
| 08/2015 |       1        |
| 09/2015 |       1        |
----------------------------

问题是,如何将结果转换如下。相反,这个表只有4行数据,如何使用total_visit = 0创建jan,feb,mar,apr ...行:

---------------------
|  Month  |  Total  |
---------------------
|   Jan   |    0    |
|   Feb   |    0    |
|   Mar   |    0    |
|   Apr   |    0    |
|   May   |    0    |
|   Jun   |    2    |
|   Jul   |    0    |
|   Aug   |    1    |
|   Sep   |    1    |
|   Oct   |    0    |
|   Nov   |    0    |
|   Dis   |    0    |
--------------------- 

3 个答案:

答案 0 :(得分:1)

我使用你自己的查询,因为它正在工作,我稍微修改了一下。请考虑以下查询:

SELECT `month`, `total_visits` FROM
(
SELECT * FROM
(
SELECT '01' AS `month_num`, 'Jan' As `month`, 0 AS `total_visits`
UNION
SELECT '02' AS `month_num`, 'Feb' AS `month`, 0 AS `total_visits`
UNION
SELECT '03' AS `month_num`, 'Mat' AS `month`, 0 AS `total_visits`
UNION
SELECT '04' AS `month_num`, 'Apr' AS `month`, 0 AS `total_visits`
UNION
SELECT '05' AS `month_num`, 'May' AS `month`, 0 AS `total_visits`
UNION
SELECT '06' AS `month_num`, 'Jun' AS `month`, 0 AS `total_visits`
UNION
SELECT '07' AS `month_num`, 'Jul' AS `month`, 0 AS `total_visits`
UNION
SELECT '08' AS `month_num`, 'Aug' AS `month`, 0 AS `total_visits`
UNION
SELECT '09' AS `month_num`, 'Sep' AS `month`, 0 AS `total_visits`
UNION
SELECT '10' AS `month_num`, 'Oct' AS `month`, 0 AS `total_visits`
UNION
SELECT '11' AS `month_num`, 'Nov' AS `month`, 0 AS `total_visits`
UNION
SELECT '12' AS `month_num`, 'Dec' AS `month`, 0 AS `total_visits`
UNION

SELECT DATE_FORMAT(last_login_dt,'%m') as `month_num`, DATE_FORMAT(last_login_dt,'%b') as `month`,
count(last_login_dt) as `total_visits` from public_user
WHERE DATE_FORMAT(last_login_dt,'%Y') = YEAR(CURDATE())
group by DATE_FORMAT(last_login_dt,'%m/%Y')
order by `total_visits` desc

) AS tmp

GROUP BY `month`
) AS total_visits
ORDER BY `month_num`;

结果是:

---------------------
|  Month  |  Total  |
---------------------
|   Jan   |    0    |
|   Feb   |    0    |
|   Mar   |    0    |
|   Apr   |    0    |
|   May   |    0    |
|   Jun   |    2    |
|   Jul   |    0    |
|   Aug   |    1    |
|   Sep   |    1    |
|   Oct   |    0    |
|   Nov   |    0    |
|   Dis   |    0    |
--------------------- 

我首先创建了显示月份及其total_visits的表(显然为0)。然后我将您的查询统一到我创建的派生表。我还添加了month_num字段,其唯一目的是按月对数据进行排序,尽管最终结果中没有显示。

答案 1 :(得分:0)

你快到了。在日期格式字符串中尝试此%b:

mysql> SELECT DATE_FORMAT(NOW(), '%b');
+--------------------------+
| DATE_FORMAT(NOW(), '%b') |
+--------------------------+
| Jun                      |
+--------------------------+
1 row in set (0.00 sec)

mysql>

答案 2 :(得分:0)

月份名称的格式为%b 。 所以我认为你必须将%m改为%b。

如果有效,请尝试使用此SQL:

SELECT DATE_FORMAT(last_login_dt,'%b/%Y') as `month`,
count(last_login_dt) as `total_visits` from public_user
WHERE DATE_FORMAT(last_login_dt,'%Y') = YEAR(CURDATE()) 
group by DATE_FORMAT(last_login_dt,'%m/%Y')
order by `month` asc;

更新时间06/11/2015 11:57 AM GMT + 8

我们无法在行中显示月份列表,而是用于创建派生列以显示月份和行中的total_visits。我使用查询的大小写来检查日期是否针对特定月份。

SELECT
CASE WHEN DATE_FORMAT(last_login_dt,'%m') = '01' THEN count(last_login_dt) ELSE '0' END AS Jan,
CASE WHEN DATE_FORMAT(last_login_dt,'%m') = '02' THEN count(last_login_dt) ELSE '0' END AS Feb,
CASE WHEN DATE_FORMAT(last_login_dt,'%m') = '03' THEN count(last_login_dt) ELSE '0' END AS Mar,
CASE WHEN DATE_FORMAT(last_login_dt,'%m') = '04' THEN count(last_login_dt) ELSE '0' END AS Apr,
CASE WHEN DATE_FORMAT(last_login_dt,'%m') = '05' THEN count(last_login_dt) ELSE '0' END AS May,
CASE WHEN DATE_FORMAT(last_login_dt,'%m') = '06' THEN count(last_login_dt) ELSE '0' END AS Jun,
CASE WHEN DATE_FORMAT(last_login_dt,'%m') = '07' THEN count(last_login_dt) ELSE '0' END AS Jul,
CASE WHEN DATE_FORMAT(last_login_dt,'%m') = '08' THEN count(last_login_dt) ELSE '0' END AS Aug,
CASE WHEN DATE_FORMAT(last_login_dt,'%m') = '09' THEN count(last_login_dt) ELSE '0' END AS Sep,
CASE WHEN DATE_FORMAT(last_login_dt,'%m') = '10' THEN count(last_login_dt) ELSE '0' END AS Oct,
CASE WHEN DATE_FORMAT(last_login_dt,'%m') = '11' THEN count(last_login_dt) ELSE '0' END AS Nov,
CASE WHEN DATE_FORMAT(last_login_dt,'%m') = '12' THEN count(last_login_dt) ELSE '0' END AS `Dec`
FROM public_user;

结果将是:

------------------------------------------------------------------------
| Jan | Feb |  Mar|  Apr|  May|  Jun|  Jul| Aug | Sep| Oct | Nov | Dec |
------------------------------------------------------------------------
|  0  |  0  |  0  |  0  |  0  |  2  |  0  |  1  |  1 |  0  |  0  |  0  |
------------------------------------------------------------------------