我想根据条件获取累积数据
示例数据集(更新):
CREATE TABLE stackoverflow (
DT date,
Part_number varchAr(10),
sales_volume int
)
insert into stackoverflow values('2018-03-01','HDD','190000');
insert into stackoverflow values('2018-03-03','HDD','20000');
insert into stackoverflow values('2018-03-03','HDD','70000');
insert into stackoverflow values('2018-03-03','SDD','100000');
insert into stackoverflow values('2018-03-03','SDD','20000');
insert into stackoverflow values('2018-03-05','HDD','90000');
insert into stackoverflow values('2018-03-05','SDD','50000');
insert into stackoverflow values('2018-03-05','SDD','50000');
insert into stackoverflow values('2018-03-06','SDD','20000');
insert into stackoverflow values('2018-03-07','HDD','50000');
insert into stackoverflow values('2018-03-07','HDD','40000');
insert into stackoverflow values('2018-03-07','SDD','100000');
insert into stackoverflow values('2018-03-07','SDD','40000');
insert into stackoverflow values('2018-03-08','HDD','20000');
insert into stackoverflow values('2018-03-08','HDD','30000');
insert into stackoverflow values('2018-03-08','SDD','60000');
insert into stackoverflow values('2018-04-08','SDD','80000');
insert into stackoverflow values('2018-04-09','HDD','10000');
insert into stackoverflow values('2018-05-11','HDD','20000');
select * from stackoverflow;
如果运行总计高于该条件,则将其重置并在以下条件下启动。
条件
cum.sum <= 200000 || "OK" is displayed on DESC when it exceeds 100,000
cum.sum <= 300000 || "OK" is displayed on DESC when it exceeds 80,000
cum.sum <= 400000 || "OK" is displayed on DESC when it exceeds 60,000
cum.sum <= 500000 || "OK" is displayed on DESC when it exceeds 40,000
cum.sum > 500000 || "OK" is displayed on DESC when it exceeds 30,000
条件结果 -描述专栏不过是对条件结果的提醒。
DT PARTS COUNT CUM.SUM DESC.
2018-03-01 HDD 190,000 190,000 OK
2018-03-03 HDD 20,000 210,000
2018-03-03 HDD 70,000 280,000
2018-03-03 SDD 100,000 100,000 OK
2018-03-03 SDD 20,000 120,000
2018-03-05 HDD 90,000 370,000 OK
2018-03-05 SDD 50,000 170,000
2018-03-05 SDD 50,000 220,000 OK
2018-03-06 SDD 20,000 240,000
2018-03-07 HDD 50,000 420,000
2018-03-07 HDD 40,000 460,000 OK
2018-03-07 SDD 100,000 340,000 OK
2018-03-07 SDD 40,000 380,000
2018-03-08 HDD 20,000 480,000
2018-03-08 HDD 30,000 510,000 OK
2018-03-08 SDD 60,000 440,000 OK
2018-04-08 SDD 80,000 520,000 OK
2018-04-09 HDD 10,000 520,000
2018-05-11 HDD 20,000 540,000 OK
所需结果
DT PARTS COUNT CUM.SUM
2018-03-01 HDD 190,000 190,000
2018-03-03 SDD 100,000 100,000
2018-03-05 HDD 90,000 370,000
2018-03-05 SDD 50,000 220,000
2018-03-07 HDD 40,000 460,000
2018-03-07 SDD 100,000 340,000
2018-03-08 HDD 30,000 510,000
2018-03-08 SDD 60,000 440,000
2018-04-08 SDD 80,000 520,000
2018-05-11 HDD 20,000 540,000
================================================ ======
更新的样本数据集和所需结果。
我将向所有有“确定”问题的人解释。
基于PARTS列的HDD,
初始值为190000,并且超过100,000,所以“确定”
条件基于最后写入“ ok”的日期的总和数据。
答案 0 :(得分:0)
除了我不理解逻辑的Desc
列(显然,您将使用一些基本的case..when
表达式)之外,以下查询可以帮助您:
select dt as "DT", Part_number as "PARTS", sales_volume as "COUNT",
sum(sales_volume) over (partition by Part_number order by dt) as "CUM.SUM",
case when sales_volume >= 100000 then 'OK' end as "DESC."
from stackoverflow
order by dt, parts
答案 1 :(得分:0)