简体   繁体   English

DB2-使用运行总计计算本月至今的销售额

[英]DB2 - Using Running totals to calculate Month To Date Sales

I'm calculating the Month to date sales using DB2 code to compare July 2019 vs July 2018 totals. 我正在使用DB2代码计算本月迄今的销售额,以比较2019年7月与2018年7月的总数。

It needs to be rolling to compare the current month of data to the same month last year, otherwise I am comparing two different periods of data. 需要比较当前月份的数据和去年同一月份的数据,否则我将比较两个不同时期的数据。

EG if it were static, I'd be comparing mtd July (2019) week 2 sales vs mtd July week 4 (2018). 例如,如果它是静态的,我将比较mtd 7月(2019)第2周的销售额与mtd 7月第4周(2018)的销售额。

I need mtd July 2019 week 2 vs mtd July (2018) week 2 mtd Sales. 我需要mtd 2019年7月第2周与mtd七月(2018)周2 mtd销售。

How can I get MTD Rolling sales in DB2?? 如何在DB2中获得MTD Rolling销售?

I've tried a static MTD - Which is ; 我已经尝试了静态MTD-这是; however, this is not the ideal method. 但是,这不是理想的方法。 For rolling, I've tried a sum() over but this returns a syntax error (-119 & -727). 对于滚动,我尝试了sum(),但这会返回语法错误(-119&-727)。

Sample table 样品表

Year |Month | 年|月|月 Week | 周| Sales 销售

2018 | 2018 | July | 七月| 1 | 1 | 2000 2000

2018 | 2018 | July | 七月| 2 | 2 | 1500 1500

2018 | 2018 | July | 七月| 3 | 3 | 1000 1000

2018 | 2018 | July | 七月| 4 | 4 | 2000 2000

2019 | 2019 | July | 七月| 1 | 1 | 1750 1750

2019 | 2019 | July | 七月| 2 | 2 | 2100 2100

Expected Results 预期成绩

Year |Month | 年|月|月 Week | 周| Sales | 销售 Running Total 总计


2018 | 2018 | July | 七月| 1 | 1 | 2000 | 2000 | 2000 2000

2018 | 2018 | July | 七月| 2 | 2 | 1500 | 1500 | 3500 3500

2018 | 2018 | July | 七月| 3 | 3 | 1000 | 1000 | 4500 4500

2018 | 2018 | July | 七月| 4 | 4 | 2000 | 2000 | 6500 6500

2019 | 2019 | July | 七月| 1 | 1 | 1750 | 1750 | 1750 1750

2019 | 2019 | July | 七月| 2 | 2 | 2100 | 2100 | 3850 3850

-- Incorrect results gathered from static -从静态收集的错误结果

Year |Month | 年|月|月 Week | 周| Sales | 销售 Running Total 总计


2018 | 2018 | July | 七月| 1 | 1 | 2000 | 2000 | 6500 6500

2018 | 2018 | July | 七月| 2 | 2 | 1500 | 1500 | 6500 6500

2018 | 2018 | July | 七月| 3 | 3 | 1000 | 1000 | 6500 6500

2018 | 2018 | July | 七月| 4 | 4 | 2000 | 2000 | 6500 6500

2019 | 2019 | July | 七月| 1 | 1 | 1750 | 1750 | 3850 3850

2019 | 2019 | July | 七月| 2 | 2 | 2100 | 2100 | 3850 3850

Code from Static (works but incorrect for comparing current month): 来自静态的代码(有效,但不能用于比较当前月份):

Select
     a.Year,
     a.Month,
     a.Week,
     sum(a.sales) as weekly
     mtd.mtd_sales as MTD
from sales a 
inner join 
     (Select
          a.Year,
          a.Month,
          sum(a.sales) as mtd_sales
      from sales a
      group by
          a.Year,
          a.Month) MTD on MTD.week = a.week
group by
     a.Year,
     a.Month,
     a.Week,
     mtd.mtd;

I've tried using the below code for rolling, but it produces error (SQL State -119 & - 727) which I believe indicates a syntax error. 我尝试使用下面的代码进行滚动,但是会产生错误(SQL状态-119和-727),我认为这表明语法错误。

Code for rolling 滚动代码

Select
  a.Year,
  a.Month,
  a.Week,
  sum(a.sales) over (Partition by week order by 
month) 
from sales a
group by
    a.year,
    a.month,
    a.week;

Simply run the window function, SUM() OVER() , at original row level and not inside an aggregate query. 只需在原始行级别而不是在聚合查询中运行窗口函数SUM() OVER() Additionally, adjust the PARTITION BY and ORDER BY clauses as your groupings should be year and month and week should be the ordering column. 此外,调整PARTITION BYORDER BY子句,因为您的分组应该是年份月份星期应该是排序列。

SELECT
  s.Year,
  s.Month,
  s.Week,
  s.Sales,
  SUM(s.sales) over (PARTITION BY s.Year, s.Month ORDER BY s.Week) as running_total
FROM sales s;

dbfiddle demo (using Db2 Developer-C 11.1 version) dbfiddle演示 (使用Db2 Developer-C 11.1版)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM