MySQL 带日期的 SUMIFS
MySQL SUMIFS with Dates
在 MySQL
上遇到 SUMIFS 场景困难
这是我的数据:
CREATE TABLE opportunities
(close_date DATE NOT NULL,
sale_amount DECIMAL NOT NULL);
INSERT INTO opportunities VALUES ('20190101',100);
INSERT INTO opportunities VALUES ('20190115',115);
INSERT INTO opportunities VALUES ('20190210',150);
INSERT INTO opportunities VALUES ('20190212',180);
INSERT INTO opportunities VALUES ('20190306',200);
INSERT INTO opportunities VALUES ('20190315',220);
INSERT INTO opportunities VALUES ('20190422',250);
INSERT INTO opportunities VALUES ('20190428',260);
INSERT INTO opportunities VALUES ('20190509',300);
INSERT INTO opportunities VALUES ('20190521',310);
INSERT INTO opportunities VALUES ('20190601',325);
INSERT INTO opportunities VALUES ('20190630',375);
INSERT INTO opportunities VALUES ('20190703',400);
INSERT INTO opportunities VALUES ('20190716',500);
INSERT INTO opportunities VALUES ('20190813',550);
INSERT INTO opportunities VALUES ('20190814',575);
INSERT INTO opportunities VALUES ('20190909',625);
INSERT INTO opportunities VALUES ('20190929',650);
INSERT INTO opportunities VALUES ('20191004',700);
INSERT INTO opportunities VALUES ('20191022',750);
INSERT INTO opportunities VALUES ('20191112',800);
INSERT INTO opportunities VALUES ('20191119',900);
INSERT INTO opportunities VALUES ('20191202',950);
INSERT INTO opportunities VALUES ('20191216',975);
我需要完成的是每个月-年有 1 行(例如 2019-01、2019-02、2019-03 等),对于该行,我需要对 sale_amount 前 6 个月。因此,2019-12 年的 sale_amount 等于 2019 年 7 月 1 日至 2019 年 12 月 31 日期间具有 close_date 的所有 sale_amount 的总和。根据我提供的数据,我需要我的最终结果如下所示:
Month Sale_Amount (Prev 6 Months)
2019-01 215
2019-02 545
2019-03 965
2019-04 1475
2019-05 2085
2019-06 2785
2019-07 3470
2019-08 4265
2019-09 5120
2019-10 6060
2019-11 7150
2019-12 8375
这是我到目前为止尝试过的:
SELECT
DATE_FORMAT(op.close_date, '%Y-%m'),
SUM(CASE
WHEN op.close_date >= DATE_FORMAT(DATE_SUB(op.close_date, INTERVAL 5 MONTH) ,'%Y-%m-01')
AND op.close_date <= LAST_DAY(op.close_date) THEN op.sale_amount
END)
FROM opportunities op
GROUP BY DATE_FORMAT(op.close_date, '%Y-%m')
ORDER BY DATE_FORMAT(op.close_date, '%Y-%m')
这行不通,它只是returns那个月的总和
我得到的实际结果:
Month Sale_Amount (Prev 6 Months)
2019-01 215
2019-02 330
2019-03 420
2019-04 510
2019-05 610
2019-06 700
2019-07 900
2019-08 1125
2019-09 1275
2019-10 1450
2019-11 1700
2019-12 1925
我正在尝试从 Excel 电子表格转换它,下面的公式完全符合我的意图,但不确定如何转换为 MySQL
SUMIFS($B:$B0, --sum range
$A:$A0,">="&DATE(YEAR(EOMONTH(A2,-6)+1),MONTH(EOMONTH(A2,-6)+1),1), --close_date >= than first day 6 months ago
$A:$A0,"<="&DATE(YEAR(A2),MONTH(A2),DAY(EOMONTH(A2,0)))) --close date <= last date of current month
希望我的问题足够清楚,提前致谢
使用window函数:
SELECT DATE_FORMAT(op.close_date, '%Y-%m'),
SUM(op.sale_amount) as this_month,
SUM(op.sale_amount) OVER (ORDER BY MIN(op.close_date) ROWS BETWEEN 5 PRECEDING AND CURRENT ROW) as running_6_months
FROM opportunities op
GROUP BY DATE_FORMAT(op.close_date, '%Y-%m')
ORDER BY DATE_FORMAT(op.close_date, '%Y-%m');
这是 MySQL < 8.0 的解决方案:
select d.close_month, sum(o.sale_amount) sale_amount
from
(select distinct date_format(close_date, '%Y-%m-01') close_month from opportunities) d
inner join opportunities o
on o.close_date < close_month + interval 1 month
and o.close_date >= close_month - interval 5 month
group by d.close_month
order by d.close_month
这通过生成不同月份的列表,将其与最后 6 个月的 table 连接起来,然后聚合来实现。
close_month | sale_amount
:---------- | ----------:
2019-01-01 | 215
2019-02-01 | 545
2019-03-01 | 965
2019-04-01 | 1475
2019-05-01 | 2085
2019-06-01 | 2785
2019-07-01 | 3470
2019-08-01 | 4265
2019-09-01 | 5120
2019-10-01 | 6060
2019-11-01 | 7150
2019-12-01 | 8375
在 MySQL
上遇到 SUMIFS 场景困难这是我的数据:
CREATE TABLE opportunities
(close_date DATE NOT NULL,
sale_amount DECIMAL NOT NULL);
INSERT INTO opportunities VALUES ('20190101',100);
INSERT INTO opportunities VALUES ('20190115',115);
INSERT INTO opportunities VALUES ('20190210',150);
INSERT INTO opportunities VALUES ('20190212',180);
INSERT INTO opportunities VALUES ('20190306',200);
INSERT INTO opportunities VALUES ('20190315',220);
INSERT INTO opportunities VALUES ('20190422',250);
INSERT INTO opportunities VALUES ('20190428',260);
INSERT INTO opportunities VALUES ('20190509',300);
INSERT INTO opportunities VALUES ('20190521',310);
INSERT INTO opportunities VALUES ('20190601',325);
INSERT INTO opportunities VALUES ('20190630',375);
INSERT INTO opportunities VALUES ('20190703',400);
INSERT INTO opportunities VALUES ('20190716',500);
INSERT INTO opportunities VALUES ('20190813',550);
INSERT INTO opportunities VALUES ('20190814',575);
INSERT INTO opportunities VALUES ('20190909',625);
INSERT INTO opportunities VALUES ('20190929',650);
INSERT INTO opportunities VALUES ('20191004',700);
INSERT INTO opportunities VALUES ('20191022',750);
INSERT INTO opportunities VALUES ('20191112',800);
INSERT INTO opportunities VALUES ('20191119',900);
INSERT INTO opportunities VALUES ('20191202',950);
INSERT INTO opportunities VALUES ('20191216',975);
我需要完成的是每个月-年有 1 行(例如 2019-01、2019-02、2019-03 等),对于该行,我需要对 sale_amount 前 6 个月。因此,2019-12 年的 sale_amount 等于 2019 年 7 月 1 日至 2019 年 12 月 31 日期间具有 close_date 的所有 sale_amount 的总和。根据我提供的数据,我需要我的最终结果如下所示:
Month Sale_Amount (Prev 6 Months)
2019-01 215
2019-02 545
2019-03 965
2019-04 1475
2019-05 2085
2019-06 2785
2019-07 3470
2019-08 4265
2019-09 5120
2019-10 6060
2019-11 7150
2019-12 8375
这是我到目前为止尝试过的:
SELECT
DATE_FORMAT(op.close_date, '%Y-%m'),
SUM(CASE
WHEN op.close_date >= DATE_FORMAT(DATE_SUB(op.close_date, INTERVAL 5 MONTH) ,'%Y-%m-01')
AND op.close_date <= LAST_DAY(op.close_date) THEN op.sale_amount
END)
FROM opportunities op
GROUP BY DATE_FORMAT(op.close_date, '%Y-%m')
ORDER BY DATE_FORMAT(op.close_date, '%Y-%m')
这行不通,它只是returns那个月的总和
我得到的实际结果:
Month Sale_Amount (Prev 6 Months)
2019-01 215
2019-02 330
2019-03 420
2019-04 510
2019-05 610
2019-06 700
2019-07 900
2019-08 1125
2019-09 1275
2019-10 1450
2019-11 1700
2019-12 1925
我正在尝试从 Excel 电子表格转换它,下面的公式完全符合我的意图,但不确定如何转换为 MySQL
SUMIFS($B:$B0, --sum range
$A:$A0,">="&DATE(YEAR(EOMONTH(A2,-6)+1),MONTH(EOMONTH(A2,-6)+1),1), --close_date >= than first day 6 months ago
$A:$A0,"<="&DATE(YEAR(A2),MONTH(A2),DAY(EOMONTH(A2,0)))) --close date <= last date of current month
希望我的问题足够清楚,提前致谢
使用window函数:
SELECT DATE_FORMAT(op.close_date, '%Y-%m'),
SUM(op.sale_amount) as this_month,
SUM(op.sale_amount) OVER (ORDER BY MIN(op.close_date) ROWS BETWEEN 5 PRECEDING AND CURRENT ROW) as running_6_months
FROM opportunities op
GROUP BY DATE_FORMAT(op.close_date, '%Y-%m')
ORDER BY DATE_FORMAT(op.close_date, '%Y-%m');
这是 MySQL < 8.0 的解决方案:
select d.close_month, sum(o.sale_amount) sale_amount
from
(select distinct date_format(close_date, '%Y-%m-01') close_month from opportunities) d
inner join opportunities o
on o.close_date < close_month + interval 1 month
and o.close_date >= close_month - interval 5 month
group by d.close_month
order by d.close_month
这通过生成不同月份的列表,将其与最后 6 个月的 table 连接起来,然后聚合来实现。
close_month | sale_amount :---------- | ----------: 2019-01-01 | 215 2019-02-01 | 545 2019-03-01 | 965 2019-04-01 | 1475 2019-05-01 | 2085 2019-06-01 | 2785 2019-07-01 | 3470 2019-08-01 | 4265 2019-09-01 | 5120 2019-10-01 | 6060 2019-11-01 | 7150 2019-12-01 | 8375