我具有以下表结构:
table = {id,DailyDate,value1,value2}与DailyDate例如。 2000-01-01

我如何才能每月分别获取每个值的摘要结果?

我正在尝试以下操作,但不起作用

Select Sum(Value1),Year(DailyDate),Month(DailyDate)
From table
Group by Year(DailyDate),Month(DailyDate)
Order by Year(DailyDate),Month(DailyDate)


ty

最佳答案

如果您要的是按年份分类的小计,请尝试以下操作:

SELECT TheSum, TheYear, COALESCE(TheMonth, CONCAT('Total Year ', TheYear))
FROM (
  SELECT
    SUM(Value1) TheSum,
    YEAR(DailyDate) AS TheYear,
    MONTH(DailyDate) AS TheMonth
  FROM table
  GROUP BY Year(DailyDate), MONTH(DailyDate) WITH ROLLUP) x
WHERE TheYear IS NOT NULL


内部查询使用按月份生成小计,按年份生成总计。外部查询会格式化年份并删除总计。

关于mysql - 每年或每月汇总的MySql查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15937864/

10-11 05:21