mysql中每个周期的总用户数

时间:2016-10-28 08:26:09

标签: mysql count case

我需要在季度注册用户的判断中看到

例如: - 季度“Jan-Mar”:20

例如: - 季度“4月 - 6月”:30

id = id_ju1
registration date =date_regis
table = master

SELECT count(DISTINCT(id_ju1)), 
CASE 
WHEN month(date_regis) >=1 and month(date_regis) <=3 THEN 'Jan-Mar' 
WHEN  month(date_regis) >=4 and month(date_regis) <=6 THEN 'Apr-Jun' END 
FROM master

2 个答案:

答案 0 :(得分:1)

您可以使用QUARTER功能。

Dates that have a month of Jan-Mar would return 1.
Dates that have a month of Apr-Jun would return 2.
Dates that have a month of Jul-Sep would return 3.
Dates that have a month of Oct-Dec would return 4.

参见:https://www.techonthenet.com/mysql/functions/quarter.php

SELECT QUARTER(date_regis) AS quarter, COUNT(DISTINCT(id_ju1)) AS idcount
FROM master 
GROUP BY QUARTER(date_regis)
ORDER BY QUARTER(date_regis)

答案 1 :(得分:0)

尝试类似下面的内容:

SELECT count(DISTINCT(id_ju1))
FROM
(
SELECT ID_JUN1,
CASE 
WHEN month(date_regis) >=1 and month(date_regis) <=3 THEN 'jun-mar'
WHEN  month(date_regis) >=4 and month(date_regis) <=6 THEN 'mar-jun' END AS Quarter
FROM master
)
GROUP BY Quarter