所以我有一个可以提供总和成本的查询:
SELECT ROUND(SUM(cost), 2) AS 'Sum of Cost',
MONTH(startDateTime) AS Month,
MONTHNAME(STR_TO_DATE(MONTH(startDateTime), '%m')) month_name
FROM Event
GROUP BY Month;
我想要添加到此查询中的另一件事是事件发生在每个月的次数,因为它们是几个月在同一天发生事件,从而影响事件的总和。
答案 0 :(得分:0)
首先按照逻辑顺序
更新您的论坛SELECT ROUND(SUM(cost), 2) AS 'Sum of Cost', MONTH(startDateTime) AS Month
FROM Event
GROUP BY MONTH(startDateTime);
答案 1 :(得分:0)
由于您按月分组,您可以在同一句话中同时使用SUM和COUNT。
SELECT ROUND(SUM(cost), 2) AS 'Sum of Cost',
MONTH(startDateTime) AS Month,
MONTHNAME(STR_TO_DATE(MONTH(startDateTime), '%m')) month_name,
COUNT(*) as NumEvents
FROM Event
GROUP BY MONTH(startDateTime);