2016-10-28 18 views
0

我需要在判決季度看到註冊用戶用戶總數在MySQL

如: - 季度「一月至三月」:20

如: - 季度「四月至六月」: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 

回答

1

您可以用四分之一的功能這一點。

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) 
+0

您好,感謝您的回答,我跑好了,我還有一個問題,也說明我季度0:當我刪除了第一個元素 –

+1

請記住這是正確的。如果你有答案。 –

+0

感謝您的回覆。爲什麼在這裏你想要零? –

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