mysql - 从特定日期 sql 算起 12 个月的总和

标签 mysql sql

我想用 SQL 计算每年的年度记录,例如 2001 年 4 月 4 日2002 年 4 月 4 日

喜欢:

Total | Year
2000  | 4th april 2001 to 4th april 2002
3000  | 4th april 2002 to 4th april 2003

等等...

最佳答案

这有点棘手。我认为最简单的方法是减去3个月3天并按年份汇总:

select year((dte - interval 3 day) - interval 3 month), sum(amount)
from t
group by year((dte - interval 3 day) - interval 3 month);

不幸的是,闰年的存在干扰了按日的计算,因为在某些年份 2 月有 28 天,而在其他年份则有 29 天。

关于mysql - 从特定日期 sql 算起 12 个月的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49723655/

相关文章:

mysql - 在全 TableView 中连接 2 列

mysql更新将重复项移动到不同的列

mysql - Magento SQL - 将所有产品名称复制到short_description属性

c# - 将日期从 mysql 表导出到 .CSV

sql - mysql 查询中的下一行和上一行

php - 如何在 Symfony2 上编写查询

php - 从mysql中的日期数据中获取按月行数

sql - SSIS : Using a variable as the name of a flat file destination

java - 如何使用 MyBatis 从 SQL 调用存储过程

mysql - 这个模式和 DBIC 代码是否有效?