【问题标题】:DB2/SQL aggregates with preceeding weekdaysDB2/SQL 聚合之前的工作日
【发布时间】:2019-09-03 00:17:55
【问题描述】:

我有一个查询,目前从预先填充的表中获取每日记录与每周数的对比:

   SELECT Employee,
          sum(case when category = 'Shirts' then daily_total else 0 end) as Shirts_DAILY,
          sum(case when category = 'Shirts' then weekly_quota else 0 end) as Shirts_QUOTA, -- this is a static column, this number is the same for every record
          sum(case when category = 'Shoes' then daily_total else 0 end) as Shoes_DAILY,
          sum(case when category = 'Shoes' then weekly_quota else 0 end) as Shoes_QUOTA, -- this is a static column, this number is the same for every record
          CURRENT_DATE as DATE_OF_REPORT
   from SalesNumbers
   where date_of_report >= current_date
   group by Employee;

每晚在脚本中运行并返回如下记录:

   Employee   |   shirts_DAILY   |   shirts_QUOTA   |   Shoes_DAILY   |   Shoes_QUOTA   |   DATE_OF_REPORT
   --------------------------------------------------------------------------------------------------------
   123                15                  75                14                85              2019-08-30

这是上周五晚间报道的记录。我正在尝试找到一种方法为每个类别添加一个列,该列将在前一个工作日(从周日到周六作为一周)每个类别的每日总数(shirts_DAILY,shoes_DAILY)的总和除以该类别的配额(衬衫_QUOTA,鞋子_QUOTA)。

例如,这里是从星期日到星期四的记录

   Employee   |   shirts_DAILY   |   shirts_QUOTA   |   Shoes_DAILY   |   Shoes_QUOTA   |   DATE_OF_REPORT
   --------------------------------------------------------------------------------------------------------
   123                15                 75                16                85              2019-08-25
   123                4                  75                2                 85              2019-08-26
   123                8                  75                6                 85              2019-08-27
   123                2                  75                8                 85              2019-08-28
   123                15                 75                14                85              2019-08-29

通过我的新更改,我希望周五晚上的记录将周日到周四的每日记录的总和除以配额(包括周五的每日总和)

周五晚上的新栏目记录:

   Employee   |   shirts_DAILY   |   shirts_QUOTA   |   shirtsPercent   |   Shoes_DAILY   |   Shoes_QUOTA   |   shoesPercent   |   DATE_OF_REPORT
   -----------------------------------------------------------------------------------------------------------------------------------------------
   123                2                 75                    61.3               7                85              62.4                2019-08-30

因此,周五的跑步为 46/75 的衬衫添加了 15,4,8,2,15,2,为 53/85 的鞋子添加了7,14,8,6,2,16。因此,如果有意义的话,就是前一周的每日总和,包括现在的每日总和。

我实现这一目标的最佳方式是什么?

【问题讨论】:

  • 查看我对您上一个问题的回答。 stackoverflow.com/questions/57726952/…。在获得每周总数后计算百分比应该不难。由于每周配额在每条记录中重复,因此仅将当天记录用于每周配额总数。
  • 它似乎仍然没有像我预期的那样工作,你是说你的答案应该达到这个结果吗?
  • 没有。你必须以我的回答为基础。我的 SQL 给出了每日总计、每周总计。对于每周配额,您必须仅使用当天记录来获取每周配额总数,因为每周配额在每条记录中都重复。

标签: sql db2


【解决方案1】:
    SELECT Employee,
    sum(case when category = 'Shirts' and  date_of_report >=  current date then 
        daily_total else 0 end) as Shirts_DAILY,
    sum(case when category = 'Shirts' and  date_of_report >=  current date then
       weekly_quota else 0 end) as Shirts_QUOTA,
    ( sum(case when category = 'Shirts' then 
        daily_total else 0 end) * 100 ) /
    ( sum(case when category = 'Shirts' and  date_of_report >=  current date then
       weekly_quota else 0 end) ) as Shirts_PERCENT,
    CURRENT_DATE as DATE_OF_REPORT
    from SalesNumbers
    where date_of_report >= ( current date - ( dayofweek(current date) - 1 ) days )
    group by Employee

【讨论】:

  • 啊,我现在明白了。在这个答案之后我修复了一点,我想我有我需要的东西。很抱歉沟通不畅,但感谢您的时间和回答!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-29
  • 2014-12-18
  • 1970-01-01
  • 2019-07-01
  • 1970-01-01
相关资源
最近更新 更多