DB2/SQL使用之前的工作日进行聚合

DB2/SQL使用之前的工作日进行聚合,sql,db2,Sql,Db2,我有一个查询,当前从预填充的表中根据每周编号获取每日记录: 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

我有一个查询,当前从预填充的表中根据每周编号获取每日记录:

   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
这是上周五晚上报道的记录。我正试图找出一种方法,为每个类别添加一列,将每个类别在前一个工作日(以星期日到星期六为一周)的每日总数(衬衫配额,鞋子配额)除以该类别的配额(衬衫配额,鞋子配额)

例如,这里有从星期天到星期四的记录

   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
。因此,如果这有意义的话,前一周的每日总和,包括今天的每日总和


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

检查我对您上一个问题的回答。在得到每周总数后,计算百分比应该不难。由于每周配额在每个记录中重复出现,因此仅对每周配额总数使用当天记录。它似乎仍然没有像我预期的那样起作用,你是说你的答案应该达到这个结果吗?不。你必须以我的答案为基础。我的SQL提供每日总计、每周总计。对于每周配额,您必须仅使用当前日期记录来获取每周配额总数,因为每周配额在每个记录中重复。啊,我现在明白了。在回答这个问题之后,我把它修好了一点,我想我已经得到了我所需要的。很抱歉沟通错误,但感谢您的时间和回答!
    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