我正在处理一个项目,以便计算每个月打开和关闭的事件数量。 我有一个记录所在的sql DB。 我试图写一个SQL请求来计算每个月打开和关闭的事件数。
到目前为止,我能够计算下面打开的事件数量:
SELECT
YEAR (`startDate`),
MONTH (`startDate`),
COUNT(id) AS Opened
FROM
incidents
GROUP BY
YEAR (`startDate`),
MONTH (`startDate`)
以下是表格的例子:
结果我需要什么。
答案 0 :(得分:0)
从测试GROUP BY月(startdate)中选择MONTHNAME(startdate),count(MONTH(startdate)),count(MONTH(enddate));
答案 1 :(得分:0)
请尝试以下查询。希望它能解决你的问题。
SELECT
MONTHNAME(start_date) as Month,
COUNT(start_date) as CountStartDate,
COUNT(end_date) as CountEndDate
FROM
incidents
GROUP BY
MONTHNAME(start_date)
答案 2 :(得分: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`)
答案 3 :(得分:0)
试试这个:
SELECT
Month(startDate) as Month,
COUNT(startDate) as startDate,
COUNT(endDate) as countEndDate
FROM
incidents
GROUP BY
Month(startDate)
答案 4 :(得分:0)
这也应该按照正确的顺序给你你想要的东西。希望它有所帮助
splice
答案 5 :(得分: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;