我正在尝试获取连续 12 个月的数据总计,但当我的字段中的值发生变化时也会发生变化。 我为表中的一个值制作了一个更简单的 sql 小提琴,用于 3 个月的运行总计。 我陷入了两个部分:1)如何获取运行总计,但每次字段值更改时都将其更改,2)如何将该运行总计写回到我的表中。 (或者,我想我可以有第二个表并写入该表。是的,这可能更容易。)
-- INIT database
CREATE TABLE Product (
Bill_Date DATE,
LGroup VARCHAR2(250 BYTE),
District VARCHAR2(50 BYTE),
MonthTot NUMBER(15,4),
Last12Tot NUMBER(18,4)
);
INSERT INTO Product(Bill_Date, LGroup, District, MonthTot) VALUES (to_date('05/01/2022','MM/DD/YYYY'), 'Sam','West', 13);
INSERT INTO Product(Bill_Date, LGroup, District, MonthTot) VALUES (to_date('06/01/2022','MM/DD/YYYY'), 'Sam','West', 10);
INSERT INTO Product(Bill_Date, LGroup, District, MonthTot) VALUES (to_date('07/01/2022','MM/DD/YYYY'), 'Sam','West', 11);
INSERT INTO Product(Bill_Date, LGroup, District, MonthTot) VALUES (to_date('08/01/2022','MM/DD/YYYY'), 'Sam','West', 8);
INSERT INTO Product(Bill_Date, LGroup, District, MonthTot) VALUES (to_date('05/01/2022','MM/DD/YYYY'), 'Chi','West', 21);
INSERT INTO Product(Bill_Date, LGroup, District, MonthTot) VALUES (to_date('06/01/2022','MM/DD/YYYY'), 'Chi','West', 19);
INSERT INTO Product(Bill_Date, LGroup, District, MonthTot) VALUES (to_date('07/01/2022','MM/DD/YYYY'), 'Chi','West', 15);
INSERT INTO Product(Bill_Date, LGroup, District, MonthTot) VALUES (to_date('08/01/2022','MM/DD/YYYY'), 'Chi','West', 11);
INSERT INTO Product(Bill_Date, LGroup, District, MonthTot) VALUES (to_date('05/01/2022','MM/DD/YYYY'), 'Emi','East', 8);
INSERT INTO Product(Bill_Date, LGroup, District, MonthTot) VALUES (to_date('06/01/2022','MM/DD/YYYY'), 'Emi','East', 5);
INSERT INTO Product(Bill_Date, LGroup, District, MonthTot) VALUES (to_date('07/01/2022','MM/DD/YYYY'), 'Emi','East', 6);
INSERT INTO Product(Bill_Date, LGroup, District, MonthTot) VALUES (to_date('08/01/2022','MM/DD/YYYY'), 'Emi','East', 4);
-- QUERY database
SELECT * FROM Product;
-- this will do a running total for one group
SELECT bill_date, LGroup, District, MonthTot
, sum(MonthTot) over (order by bill_date, LGroup, District rows between 2 preceding and current row) as Last12Tot
from (
select bill_date, LGroup, District, MonthTot
from Product
where LGroup = 'Sam'
)
order by LGroup, District, bill_date
;
所以我的数据集看起来像这样:
BILL_DATE LGROUP DISTRICT MONTHTOT
01-MAY-22 Sam West 13
01-JUN-22 Sam West 10
01-JUL-22 Sam West 11
01-AUG-22 Sam West 8
01-MAY-22 Chi West 21
01-JUN-22 Chi West 19
01-JUL-22 Chi West 15
01-AUG-22 Chi West 11
01-MAY-22 Emi East 8
01-JUN-22 Emi East 5
01-JUL-22 Emi East 6
01-AUG-22 Emi East 4
我当前的查询返回此:
BILL_DATE LGROUP DISTRICT MONTHTOT LAST12TOT
01-MAY-22 Sam West 13 13
01-JUN-22 Sam West 10 23
01-JUL-22 Sam West 11 34
01-AUG-22 Sam West 8 29
我不会在多个区中拥有一个 LGroup,因此 LGroup 本质上是我需要进行分组的依据。没有主键 - 它已经是通过查询组装的数据,以获取每个人和每个地区每月的总数。
每次我尝试获取所有组时,我的分区根本不起作用。 我有多年的经验,想要一个 12 个月的滚动总数,但仅限于每个组内。
一旦有了这个,我就可以将我的小组总数相加以获得地区总数。
使用
PARTITION BY
和 RANGE
窗口:
SELECT bill_date,
LGroup,
District,
MonthTot,
sum(MonthTot) over (
PARTITION BY LGroup
order by bill_date, District
RANGE BETWEEN INTERVAL '12' MONTH PRECEDING AND CURRENT ROW
) AS Last12Tot
FROM Product
order by LGroup, District, bill_date;