使用无限制的前一个计算运行总计

时间:2019-05-22 10:51:06

标签: sql oracle

我正在编写一个查询,该查询将给出开始日期剩余的小时数,并且应该逐渐减少到结束日期并达到零。

我尝试使用无界的先前函数。它有所帮助,但并不完全。

SELECT project_id,
       sprint_id,
       est_task_per_sprint,
       start_date,end_date,
         nvl(sum(est_task_per_sprint) over (
           order by end_date rows between unbounded preceding and 1 preceding

       ),0) tot
FROM   project_sprint where project_id=1;

sum(est_task_per_sprint)=1134是总小时数。

我得到的输出:

Proj  Sprint  Hours  startdate  enddate    hoursremaining
   1       1    262  01-JAN-19  31-JAN-19               0
   1      11    263  01-FEB-19  28-FEB-19             262
   1      21    266  01-MAR-19  31-MAR-19             525
   1      31    262  01-APR-19  30-APR-19             791
   1      41    261  01-MAY-19  31-MAY-19            1053

预期:

Proj  Sprint  Hours  startdate  enddate    hoursremaining
   1       1    262  01-JAN-19  31-JAN-19            1053
   1      11    263  01-FEB-19  28-FEB-19             791
   1      21    266  01-MAR-19  31-MAR-19             525
   1      31    262  01-APR-19  30-APR-19             262
   1      41    261  01-MAY-19  31-MAY-19               0

1 个答案:

答案 0 :(得分:0)

您可以通过从项目的总小时数中减去当前行(直到包括当前行(之前的 not 1个))的总小时数来计算剩余小时数。

我已经添加了按项目ID进行分区的功能,因此它也可以在没有ID过滤器的情况下工作,并且由于窗口现在与默认值匹配(行在无限制的上一行和当前行之间),因此我省略了该子句。此示例包括总时数和完成时数,以使其更清楚-在您的实际查询中不需要。

SELECT project_id,
       sprint_id,
       est_task_per_sprint as hours,
       start_date,
       end_date,
       sum(est_task_per_sprint) over (partition by project_id) as total,
       coalesce(sum(est_task_per_sprint)
                  over (partition by project_id order by end_date), 0) as completed,
       sum(est_task_per_sprint) over (partition by project_id)
         - coalesce(sum(est_task_per_sprint)
                      over (partition by project_id order by end_date), 0) as remaining
FROM   project_sprint
--WHERE  project_id=1
ORDER BY project_id, start_date;

PROJECT_ID  SPRINT_ID      HOURS START_DATE END_DATE        TOTAL  COMPLETED  REMAINING
---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
         1          1        262 2019-01-01 2019-01-31       1314        262       1052
         1         11        263 2019-02-01 2019-02-28       1314        525        789
         1         21        266 2019-03-01 2019-03-31       1314        791        523
         1         31        262 2019-04-01 2019-04-30       1314       1053        261
         1         41        261 2019-05-01 2019-05-31       1314       1314          0

这与对以后行进行求和相同,这次使用的窗口子句的行数介于1个跟无限制的跟在以下之间:

SELECT project_id,
       sprint_id,
       est_task_per_sprint as hours,
       start_date,
       end_date,
       coalesce(sum(est_task_per_sprint)
                over (partition by project_id order by end_date
                      rows between 1 following and unbounded following), 0) as remaining
FROM   project_sprint
--WHERE  project_id=1
ORDER BY project_id, start_date;

PROJECT_ID  SPRINT_ID      HOURS START_DATE END_DATE    REMAINING
---------- ---------- ---------- ---------- ---------- ----------
         1          1        262 2019-01-01 2019-01-31       1052
         1         11        263 2019-02-01 2019-02-28        789
         1         21        266 2019-03-01 2019-03-31        523
         1         31        262 2019-04-01 2019-04-30        261
         1         41        261 2019-05-01 2019-05-31          0

db<>fiddle

请注意,这些数字与您的预期结果不完全匹配,但这是因为我相信您的期望并不完全正确,并且基于您查询产生的数字-只是反转这些数字是没有道理的。