如何按条件获取动态累积数据

时间:2019-07-17 04:17:46

标签: sql sql-server sql-server-2017

我想根据条件获取累积数据

示例数据集(更新):

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;

如果运行总计高于该条件,则将其重置并在以下条件下启动。

条件

  1. 如果系统小于或等于200,000,
    值大于100000时,“备注”列中将显示“确定”。
  2. 如果系统小于或等于30,000,
    值大于80000时,“备注”列中将显示“确定”。
  3. 如果粘合系统小于或等于400,000,
    值大于60,000时,“备注”列中将显示“确定”。
  4. 如果系统小于或等于500,000,
    值大于40000时,“备注”列中将显示“确定”。
  5. 如果系统大于500,000,
    每次超过30000时,备注栏中将显示“确定”。
    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,

Example : HDD of the PARTS column

  1. 初始值为190000,并且超过100,000,所以“确定”

  2. 条件基于最后写入“ ok”的日期的总和数据。

2 个答案:

答案 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

Demo

答案 1 :(得分:0)

这可能会有所帮助,但Desc部分除外:

;WITH RuningTotal
AS(
select ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS SL
       ,*
from stackoverflow
)
SELECT * ,SUM (sales_volume) OVER (PARTITION BY Part_number ORDER BY SL,DT,Part_number) AS RunningTotal
FROM RuningTotal
ORDER BY DT,Part_number

与输出和预期结果的比较。

enter image description here