2016-11-23 58 views
-2

我正在開展一個項目,以計算每個月打開和關閉的事件數量。 我有一個SQL DB的記錄。 我正在嘗試編寫一個SQL請求來計算每個月打開和關閉的事件數量。MySql - 按表格中的2個不同日期計算月份數

到目前爲止我能計算事件的數量打開如下圖所示:

SELECT 
    YEAR (`startDate`), 
    MONTH (`startDate`), 
    COUNT(id) AS Opened 
FROM 
    incidents 
GROUP BY 
    YEAR (`startDate`), 
    MONTH (`startDate`) 

下面是表的爲例我有:

enter image description here

而結果我需要的。

enter image description here

+0

歡迎來到Stack Overflow。請查看[如何創建一個最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve)。請包括您嘗試過的代碼以及您遇到的具體問題。當您可以在問題中重現文本中的數據時,不要鏈接到圖像。 –

回答

0

選擇MONTHNAME(開始日期),計數(個月(開始日期)),從試驗組計數(個月(結束日期))BY月(開始日期);

0

請嘗試以下查詢。希望它能解決你的問題。

SELECT 
    MONTHNAME(start_date) as Month, 
    COUNT(start_date) as CountStartDate, 
    COUNT(end_date) as CountEndDate 
FROM 
    incidents 
GROUP BY 
    MONTHNAME(start_date) 
0

希望這會有所幫助。

SELECT 
    YEAR (`startDate`), 
    MONTH (`endDate`), 
    COUNT(id) AS Opened 
FROM 
    incidents 
WHERE 
    YEAR (`startDate`)= YEAR (`endDate`) 
    MONTH (`startDate`)= MONTH (`endDate`) 
GROUP BY 
    YEAR (`startDate`), 
    MONTH (`endDate`) 
0

試試這個:

SELECT Month(startDate) as Month, COUNT(startDate) as startDate, COUNT(endDate) as countEndDate FROM incidents GROUP BY Month(startDate)

0

這應該給你你按照正確的順序要和什麼。希望它有幫助

SELECT 
    YEAR('startDate'), 
    DATENAME(Month,'startDate'), 
    COUNT(*) 
FROM incidents 
GROUP BY 
    YEAR('startDate'), 
    MONTH('startDate'), 
    DATENAME(Month,'startDate') 
ORDER BY 
    YEAR('startDate'), 
    MONTH('startDate') 
0

試試看,如果這工作。

CREATE TABLE abcd 
    SELECT 
     sum(if(month(start_date)=1, 1, 0)) s_1, 
     sum(if(month(start_date)=2, 1, 0)) s_2, 
     sum(if(month(start_date)=3, 1, 0)) s_3, 
     sum(if(month(start_date)=4, 1, 0)) s_4, 
     sum(if(month(start_date)=5, 1, 0)) s_5, 
     sum(if(month(start_date)=6, 1, 0)) s_6, 
     sum(if(month(start_date)=7, 1, 0)) s_7, 
     sum(if(month(start_date)=8, 1, 0)) s_8, 
     sum(if(month(start_date)=9, 1, 0)) s_9, 
     sum(if(month(start_date)=10, 1, 0)) s_10, 
     sum(if(month(start_date)=11, 1, 0)) s_11, 
     sum(if(month(start_date)=12, 1, 0)) s_12, 
     sum(if(month(end_date)=1, 1, 0)) e_1, 
     sum(if(month(end_date)=2, 1, 0)) e_2, 
     sum(if(month(end_date)=3, 1, 0)) e_3, 
     sum(if(month(end_date)=4, 1, 0)) e_4, 
     sum(if(month(end_date)=5, 1, 0)) e_5, 
     sum(if(month(end_date)=6, 1, 0)) e_6, 
     sum(if(month(end_date)=7, 1, 0)) e_7, 
     sum(if(month(end_date)=8, 1, 0)) e_8, 
     sum(if(month(end_date)=9, 1, 0)) e_9, 
     sum(if(month(end_date)=10, 1, 0)) e_10, 
     sum(if(month(end_date)=11, 1, 0)) e_11, 
     sum(if(month(end_date)=12, 1, 0)) e_12 
    FROM incidents; 

SELECT s_1 start, e_1 end, 'Jan' month FROM abcd 
UNION SELECT s_2 start, e_2 end, 'Feb' month FROM abcd 
UNION SELECT s_3 start, e_3 end, 'Mar' month FROM abcd 
UNION SELECT s_4 start, e_4 end, 'Apr' month FROM abcd 
UNION SELECT s_5 start, e_5 end, 'May' month FROM abcd 
UNION SELECT s_6 start, e_6 end, 'Jun' month FROM abcd 
UNION SELECT s_7 start, e_7 end, 'Jul' month FROM abcd 
UNION SELECT s_8 start, e_8 end, 'Aug' month FROM abcd 
UNION SELECT s_9 start, e_9 end, 'Sep' month FROM abcd 
UNION SELECT s_10 start, e_10 end, 'Oct' month FROM abcd 
UNION SELECT s_11 start, e_11 end, 'Nov' month FROM abcd 
UNION SELECT s_12 start, e_12 end, 'Dec' month FROM abcd; 

DROP TABLE abcd; 
相關問題