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

时间:2019-09-03 00:17:55

标签: 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 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

因此,星期五的跑步中,15,4,8,2,15,2的衬衫价格为46/75,7,14,8,6,2,16的鞋价格为53/85。因此,如果有道理的话,前一周的每一天的每日总和,包括当日的每日总计。

实现此目标的最佳方法是什么?

1 个答案:

答案 0 :(得分: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