Oracle SQL Aggregate减去运行总计

时间:2015-03-09 14:41:49

标签: sql oracle

美好的一天!我正在编写Oracle SQL语句以在计划中创建任务燃尽图表。我将日期截断到几周,并计算基线中的任务数,实际执行的任务数以及每周剩余的任务数。然后我运行每个的总数来向我展示" burndwown"折线图上的线条。

这是我的麻烦......对于一个部分完成的项目,ACTUAL的运行总数并不是我所期望的。我已经尝试将总和超过基线并从中减去ACTCNT。我尝试过总和超过基线,减去实际的总和。这些都没有给我我想要的东西。任何想法和帮助将不胜感激。

以下是我目前的输出:

APOID   WEEK        BASELINE    ACTCNT  ACTUAL  REMCNT  REMAINING
34912   2014/07/29  33          1       25      0   
34912   2014/08/05  32          1       24      0   
34912   2014/08/08  31          1       23      0   
34912   2014/08/15  30          1       22      0   
34912   2014/08/22  30          1       21      0   
34912   2014/08/29  29          0       20      0   
34912   2014/09/15  28          1       20      0   
34912   2014/09/22  27          0       19      0   
34912   2014/09/29  26          1       19      0   
34912   2014/10/08  26          2       18      0   
34912   2014/10/22  23          1       16      0   
34912   2014/10/29  23          1       15      0   
34912   2014/11/05  22          1       14      0   
34912   2014/11/29  21          1       13      0   
34912   2014/12/08  20          2       12      0   
34912   2015/01/08  18          0       10      0   
34912   2015/02/05  17          0       10      0   
34912   2015/02/08  14          1       10      0   
34912   2015/02/15  14          2       9       0   
34912   2015/02/22  11          1       7       0   
34912   2015/03/01  11          4       6       0   
34912   2015/03/08  11          2       2       0   
34912   2015/03/15  8           0               1       11
34912   2015/03/22  8           0               3       10
34912   2015/04/05  6           0               1       7
34912   2015/04/08  5           0               1       6
34912   2015/04/22  4           0               2       5
34912   2015/05/06  3           0               1       3
34912   2015/06/22  2           0               2       2

ACTUAL列正如SQL所说的那样计算。但是,根据这个燃尽图表的意图,Baseline和Actual应该几乎以相同的数字开始。因此,第一周将显示33的基线,并且因为一个在第一周完成,实际列应该显示32.然后剩余应该在实际离开时根据实际已完成的内容离开剩余金额。 :

APOID   WEEK        BASELINE    ACTCNT  ACTUAL  REMCNT  REMAINING
34912   2014/07/29  33          1       32      0   
34912   2014/08/05  32          1       31      0   
34912   2014/08/08  31          1       30      0   
34912   2014/08/15  30          1       29      0   
34912   2014/08/22  30          1       28      0   
34912   2014/08/29  29          0       28      0   
34912   2014/09/15  28          1       27      0   
34912   2014/09/22  27          0       27      0   
34912   2014/09/29  26          1       26      0   
34912   2014/10/08  26          2       24      0   
34912   2014/10/22  23          1       23      0   
34912   2014/10/29  23          1       22      0   
34912   2014/11/05  22          1       21      0   
34912   2014/11/29  21          1       20      0   
34912   2014/12/08  20          2       18      0   
34912   2015/01/08  18          0       18      0   
34912   2015/02/05  17          0       18      0   
34912   2015/02/08  14          1       17      0   
34912   2015/02/15  14          2       15      0   
34912   2015/02/22  11          1       14      0   
34912   2015/03/01  11          4       10      0   
34912   2015/03/08  11          2       8       0   
34912   2015/03/15  8           0               1       11
34912   2015/03/22  8           0               3       10
34912   2015/04/05  6           0               1       7
34912   2015/04/08  5           0               1       6
34912   2015/04/22  4           0               2       5
34912   2015/05/06  3           0               1       3
34912   2015/06/22  2           0               2       2
SELECT
    APOID,
    WEEK,
    SUM(BASELINE) OVER (ORDER BY WEEK DESC) BASELINE,
    ACTUAL AS ACTCNT,
    (CASE
      WHEN WEEK < DD THEN SUM(ACTUAL) OVER (ORDER BY WEEK DESC)
      ELSE NULL
    END) AS ACTUAL,
    REMAINING AS REMCNT,
    (CASE
      WHEN WEEK >= DD THEN SUM(REMAINING) OVER (ORDER BY WEEK DESC)
      ELSE NULL
    END) AS REMAINING
FROM
(
WITH 
BF AS (SELECT A.PROJECTOBJECTID AS APOID, COUNT (A.OBJECTID) AS CNT, TO_CHAR(TRUNC(A.BASELINEFINISHDATE,'W')+7,'YYYY/MM/DD') AS WEEK, TO_CHAR(TRUNC(A.DATADATE,'W')+7,'YYYY/MM/DD') AS DD
  FROM PXRPTUSER.ACTIVITY A
 WHERE (A.PROJECTOBJECTID = :POID) AND A.TYPE IN ('Task Dependent','Resource Dependent')
GROUP BY A.PROJECTOBJECTID, TO_CHAR(TRUNC(A.BASELINEFINISHDATE,'W')+7,'YYYY/MM/DD'), TO_CHAR(TRUNC(A.DATADATE,'W')+7,'YYYY/MM/DD')),

AF AS (SELECT A.PROJECTOBJECTID AS APOID, COUNT (A.OBJECTID) AS CNT, TO_CHAR(TRUNC(A.ACTUALFINISHDATE,'W')+7,'YYYY/MM/DD') AS WEEK, TO_CHAR(TRUNC(A.DATADATE,'W')+7,'YYYY/MM/DD') AS DD
  FROM PXRPTUSER.ACTIVITY A
 WHERE (A.PROJECTOBJECTID = :POID) AND A.TYPE IN ('Task Dependent','Resource Dependent')
GROUP BY A.PROJECTOBJECTID, TO_CHAR(TRUNC(A.ACTUALFINISHDATE,'W')+7,'YYYY/MM/DD'), TO_CHAR(TRUNC(A.DATADATE,'W')+7,'YYYY/MM/DD')),

PF AS (SELECT A.PROJECTOBJECTID AS APOID, COUNT (A.OBJECTID) AS CNT, TO_CHAR(TRUNC(A.REMAININGEARLYFINISHDATE,'W')+7,'YYYY/MM/DD') AS WEEK, TO_CHAR(TRUNC(A.DATADATE,'W')+7,'YYYY/MM/DD') AS DD
  FROM PXRPTUSER.ACTIVITY A
 WHERE (A.PROJECTOBJECTID = :POID) AND A.TYPE IN ('Task Dependent','Resource Dependent')
GROUP BY A.PROJECTOBJECTID, TO_CHAR(TRUNC(A.REMAININGEARLYFINISHDATE,'W')+7,'YYYY/MM/DD'), TO_CHAR(TRUNC(A.DATADATE,'W')+7,'YYYY/MM/DD'))

SELECT
  APOID, WEEK, DD, NVL(BF.CNT, 0) AS BASELINE, NVL(AF.CNT, 0) AS ACTUAL, 
    NVL(PF.CNT, 0) AS REMAINING
  FROM BF
    FULL JOIN AF USING (APOID,WEEK,DD)
    FULL JOIN PF USING (APOID,WEEK,DD)
  WHERE WEEK IS NOT NULL
  ORDER BY WEEK
) R
ORDER BY WEEK

以下是数据:

APOID   OBJECTID    BASELINEFINISHDATE  DATADATE    ACTUALFINISHDATE    REMAININGEARLYFINISHDATE    TYPE
34912   4949302     3/18/2015           3/9/2015                        3/18/2015                   Task Dependent
34912   4949322     1/30/2015           3/9/2015    2/19/2015                                       Task Dependent
34912   4949323     9/17/2014           3/9/2015    9/26/2014                                       Task Dependent
34912   4949324     1/30/2015           3/9/2015                        3/13/2015                   Task Dependent
34912   4949348     3/6/2015            3/9/2015    2/25/2015                                       Task Dependent
34912   4949349     3/6/2015            3/9/2015    3/5/2015                                        Task Dependent
34912   4949351     1/2/2015            3/9/2015    2/13/2015                                       Task Dependent
34912   4949353     2/13/2015           3/9/2015    3/2/2015                                        Task Dependent
34912   4949355     1/30/2015           3/9/2015    2/23/2015                                       Task Dependent
34912   4949400     2/10/2015           3/9/2015    2/10/2015                                       Task Dependent
34912   4949681     2/13/2015           3/9/2015    2/3/2015                                        Task Dependent
34912   4949286     10/29/2014          3/9/2015    10/29/2014                                      Task Dependent
34912   4949287     4/2/2015            3/9/2015                        4/2/2015                    Task Dependent
34912   4949289     7/22/2014           3/9/2015    7/22/2014                                       Task Dependent
34912   4949321     10/1/2014           3/9/2015    10/16/2014                                      Task Dependent
34912   4949365     10/27/2014          3/9/2015    10/27/2014                                      Task Dependent
34912   4949372     3/6/2015            3/9/2015                        3/16/2015                   Task Dependent
34912   4949374     4/16/2015           3/9/2015                        4/16/2015                   Task Dependent
34912   4949375     3/20/2015           3/9/2015                        4/15/2015                   Task Dependent
34912   4949377     9/10/2014           3/9/2015    8/20/2014                                       Task Dependent
34912   4949395     12/3/2014           3/9/2015    12/4/2014                                       Task Dependent
34912   4949396     3/31/2015           3/9/2015                        3/31/2015                   Task Dependent
34912   4949397     6/19/2015           3/9/2015                        6/19/2015                   Task Dependent
34912   4949399     4/30/2015           3/9/2015                        4/30/2015                   Task Dependent
34912   4949454     11/26/2014          3/9/2015    11/26/2014                                      Task Dependent
34912   4949456     10/6/2014           3/9/2015    10/3/2014                                       Task Dependent
34912   4949460     8/27/2014           3/9/2015    8/8/2014                                        Task Dependent
34912   4949275     8/1/2014            3/9/2015    8/5/2014                                        Task Dependent
34912   4949277     7/30/2014           3/9/2015    7/30/2014                                       Task Dependent
34912   4949278     12/3/2014           3/9/2015    12/3/2014                                       Task Dependent
34912   4949457     8/20/2014           3/9/2015    9/10/2014                                       Task Dependent
34912   4949458     10/1/2014           3/9/2015    10/2/2014                                       Task Dependent
34912   4949535     6/19/2015           3/9/2015                        6/19/2015                   Task Dependent
34912   5914141                         3/9/2015    2/27/2015                                       Task Dependent
34912   5931635                         3/9/2015                        3/16/2015                   Task Dependent
34912   5914140                         3/9/2015    2/23/2015                                       Task Dependent

这是CREATE TABLE查询...

CREATE TABLE TEMP
(
  APOID                     NUMBER,
  OBJECTID                  NUMBER,
  BASELINEFINISHDATE        DATE,
  DATADATE                  DATE,
  REMAININGEARLYFINISHDATE  DATE,
  TYPE                      VARCHAR(24)
)

这是INSERT查询...

SET DEFINE OFF;
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, REMAININGEARLYFINISHDATE, 
    TYPE)
 Values
   (34912, 4949302, TO_DATE('03/18/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/18/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, ACTUALFINISHDATE, 
    TYPE)
 Values
   (34912, 4949322, TO_DATE('01/30/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('02/19/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, ACTUALFINISHDATE, 
    TYPE)
 Values
   (34912, 4949323, TO_DATE('09/17/2014 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('09/26/2014 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, REMAININGEARLYFINISHDATE, 
    TYPE)
 Values
   (34912, 4949324, TO_DATE('01/30/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/13/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, ACTUALFINISHDATE, 
    TYPE)
 Values
   (34912, 4949348, TO_DATE('03/06/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('02/25/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, ACTUALFINISHDATE, 
    TYPE)
 Values
   (34912, 4949349, TO_DATE('03/06/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/05/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, ACTUALFINISHDATE, 
    TYPE)
 Values
   (34912, 4949351, TO_DATE('01/02/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('02/13/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, ACTUALFINISHDATE, 
    TYPE)
 Values
   (34912, 4949353, TO_DATE('02/13/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/02/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, ACTUALFINISHDATE, 
    TYPE)
 Values
   (34912, 4949355, TO_DATE('01/30/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('02/23/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, ACTUALFINISHDATE, 
    TYPE)
 Values
   (34912, 4949400, TO_DATE('02/10/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('02/10/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, ACTUALFINISHDATE, 
    TYPE)
 Values
   (34912, 4949681, TO_DATE('02/13/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('02/03/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, ACTUALFINISHDATE, 
    TYPE)
 Values
   (34912, 4949275, TO_DATE('08/01/2014 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('08/05/2014 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, ACTUALFINISHDATE, 
    TYPE)
 Values
   (34912, 4949277, TO_DATE('07/30/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('07/30/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, ACTUALFINISHDATE, 
    TYPE)
 Values
   (34912, 4949278, TO_DATE('12/03/2014 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('12/03/2014 17:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, ACTUALFINISHDATE, 
    TYPE)
 Values
   (34912, 4949286, TO_DATE('10/29/2014 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('10/29/2014 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, REMAININGEARLYFINISHDATE, 
    TYPE)
 Values
   (34912, 4949287, TO_DATE('04/02/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('04/02/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, ACTUALFINISHDATE, 
    TYPE)
 Values
   (34912, 4949289, TO_DATE('07/22/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('07/22/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, ACTUALFINISHDATE, 
    TYPE)
 Values
   (34912, 4949321, TO_DATE('10/01/2014 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('10/16/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, ACTUALFINISHDATE, 
    TYPE)
 Values
   (34912, 4949365, TO_DATE('10/27/2014 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('10/27/2014 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, REMAININGEARLYFINISHDATE, 
    TYPE)
 Values
   (34912, 4949372, TO_DATE('03/06/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/16/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, REMAININGEARLYFINISHDATE, 
    TYPE)
 Values
   (34912, 4949374, TO_DATE('04/16/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('04/16/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, REMAININGEARLYFINISHDATE, 
    TYPE)
 Values
   (34912, 4949375, TO_DATE('03/20/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('04/15/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, ACTUALFINISHDATE, 
    TYPE)
 Values
   (34912, 4949377, TO_DATE('09/10/2014 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('08/20/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, ACTUALFINISHDATE, 
    TYPE)
 Values
   (34912, 4949395, TO_DATE('12/03/2014 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('12/04/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, REMAININGEARLYFINISHDATE, 
    TYPE)
 Values
   (34912, 4949396, TO_DATE('03/31/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/31/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, REMAININGEARLYFINISHDATE, 
    TYPE)
 Values
   (34912, 4949397, TO_DATE('06/19/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('06/19/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, REMAININGEARLYFINISHDATE, 
    TYPE)
 Values
   (34912, 4949399, TO_DATE('04/30/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('04/30/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, ACTUALFINISHDATE, 
    TYPE)
 Values
   (34912, 4949454, TO_DATE('11/26/2014 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('11/26/2014 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, ACTUALFINISHDATE, 
    TYPE)
 Values
   (34912, 4949456, TO_DATE('10/06/2014 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('10/03/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, ACTUALFINISHDATE, 
    TYPE)
 Values
   (34912, 4949457, TO_DATE('08/20/2014 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('09/10/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, ACTUALFINISHDATE, 
    TYPE)
 Values
   (34912, 4949458, TO_DATE('10/01/2014 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('10/02/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, ACTUALFINISHDATE, 
    TYPE)
 Values
   (34912, 4949460, TO_DATE('08/27/2014 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('08/08/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, BASELINEFINISHDATE, DATADATE, REMAININGEARLYFINISHDATE, 
    TYPE)
 Values
   (34912, 4949535, TO_DATE('06/19/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('06/19/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), 
    'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, DATADATE, ACTUALFINISHDATE, TYPE)
 Values
   (34912, 5914140, TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('02/23/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), 'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, DATADATE, ACTUALFINISHDATE, TYPE)
 Values
   (34912, 5914141, TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('02/27/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 'Task Dependent');
Insert into TEMP
   (APOID, OBJECTID, DATADATE, REMAININGEARLYFINISHDATE, TYPE)
 Values
   (34912, 5931635, TO_DATE('03/09/2015 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/16/2015 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), 'Task Dependent');
COMMIT;

1 个答案:

答案 0 :(得分:2)

  

我已经尝试将总和超过基线并从中减去ACTCNT。我已经尝试将总和超过基线并减去实际的总和。

看起来你正在为两种方法总结错误的窗口。您需要整个范围内的基线总和,您可以从中获得 无界的前/后窗;并且实际计数的总和必须按日期升序排列,而不是降序:

SELECT
    APOID,
    WEEK,
    SUM(BASELINE) OVER (ORDER BY WEEK DESC) BASELINE,
    ACTUAL AS ACTCNT,
    (CASE
      WHEN WEEK < DD THEN
        SUM(BASELINE) OVER (ORDER BY WEEK
          RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
            - SUM(ACTUAL) OVER (ORDER BY WEEK)
      ELSE NULL
    END) AS ACTUAL,
    REMAINING AS REMCNT,
    (CASE
      WHEN WEEK >= DD THEN
        SUM(REMAINING) OVER (ORDER BY WEEK DESC)
      ELSE NULL
    END) AS REMAINING
FROM
(
...

获得:

     APOID WEEK         BASELINE     ACTCNT     ACTUAL     REMCNT  REMAINING
---------- ---------- ---------- ---------- ---------- ---------- ----------
     34912 2014/07/29         33          1         32          0            
     34912 2014/08/05         32          1         31          0            
     34912 2014/08/08         31          1         30          0            
     34912 2014/08/15         30          1         29          0            
     34912 2014/08/22         30          1         28          0            
     34912 2014/08/29         29          0         28          0            
     34912 2014/09/15         28          1         27          0            
     34912 2014/09/22         27          0         27          0            
     34912 2014/09/29         26          1         26          0            
     34912 2014/10/08         26          2         24          0            
     34912 2014/10/22         23          1         23          0            
     34912 2014/10/29         23          1         22          0            
     34912 2014/11/05         22          1         21          0            
     34912 2014/11/29         21          1         20          0            
     34912 2014/12/08         20          2         18          0            
     34912 2015/01/08         18          0         18          0            
     34912 2015/02/05         17          0         18          0            
     34912 2015/02/08         14          1         17          0            
     34912 2015/02/15         14          2         15          0            
     34912 2015/02/22         11          1         14          0            
     34912 2015/03/01         11          4         10          0            
     34912 2015/03/08         11          2          8          0            
     34912 2015/03/15          8          0                     1         11 
     34912 2015/03/22          8          0                     3         10 
     34912 2015/04/05          6          0                     1          7 
     34912 2015/04/08          5          0                     1          6 
     34912 2015/04/22          4          0                     2          5 
     34912 2015/05/06          3          0                     1          3 
     34912 2015/06/22          2          0                     2          2 

 29 rows selected 

你选择截断目标的'W'似乎很奇怪;你正在调整日历月内的周数,这似乎没什么帮助。出于分组目的,WW或IW可能更有意义。

为了理解你在做什么,我把它打破了一点点不同;这基本上是相同的方法,但有一个比现在真正需要的水平,并与'W'周组获得相同的答案;为了比较,这里是'IW'周组:

WITH b AS (
  SELECT projectobjectid, objectid, datadate, baselinefinishdate,
    TRUNC(baselinefinishdate, 'IW') + 7 AS week
  FROM temp
  WHERE projectobjectid = :poid
  AND type IN ('Task Dependent','Resource Dependent')
  AND baselinefinishdate IS NOT NULL
),
a AS (
  SELECT projectobjectid, objectid, datadate, actualfinishdate,
    TRUNC(actualfinishdate, 'IW') + 7 AS week
  FROM temp
  WHERE projectobjectid = :poid
  AND type IN ('Task Dependent','Resource Dependent')
  AND actualfinishdate IS NOT NULL
),
r AS (
  SELECT projectobjectid, objectid, datadate, remainingearlyfinishdate,
    TRUNC(remainingearlyfinishdate, 'IW') + 7 AS week
  FROM temp
  WHERE projectobjectid = :poid
  AND type IN ('Task Dependent','Resource Dependent')
  AND remainingearlyfinishdate IS NOT NULL
),
w AS (
  SELECT projectobjectid, objectid, datadate, week FROM b
  UNION SELECT projectobjectid, objectid, datadate, week FROM a
  UNION SELECT projectobjectid, objectid, datadate, week FROM r
),
s AS (
  SELECT w.projectobjectid, w.week, w.datadate,
    COUNT(b.objectid) AS bcnt, COUNT(a.objectid) AS acnt, COUNT(r.objectid) AS rcnt
  FROM w
  LEFT JOIN b ON b.projectobjectid = w.projectobjectid
    AND b.objectid = w.objectid AND b.datadate = w.datadate AND b.week = w.week
  LEFT JOIN a ON a.projectobjectid = w.projectobjectid
    AND a.objectid = w.objectid AND a.datadate = w.datadate AND a.week = w.week
  LEFT JOIN r ON r.projectobjectid = w.projectobjectid
    AND r.objectid = w.objectid AND r.datadate = w.datadate AND r.week = w.week
  GROUP BY w.projectobjectid, w.datadate, w.week
)
SELECT projectobjectid AS apoid, TO_CHAR(week, 'YYYY/MM/DD') AS week,
  SUM(bcnt) OVER (PARTITION BY projectobjectid ORDER BY week DESC) AS baseline,
  SUM(acnt) OVER (PARTITION BY projectobjectid, week) AS actcnt,
  CASE WHEN week < datadate THEN
    SUM(bcnt) OVER (PARTITION BY projectobjectid ORDER BY week
          ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
      - SUM(acnt) OVER (PARTITION BY projectobjectid ORDER BY week) END AS actual,
  SUM(rcnt) OVER (PARTITION BY projectobjectid, week) AS remcnt,
  CASE WHEN week >= datadate THEN
    SUM(rcnt) OVER (PARTITION BY projectobjectid ORDER BY week DESC) END AS remaining
FROM s
ORDER BY projectobjectid, week;

     APOID WEEK         BASELINE     ACTCNT     ACTUAL     REMCNT  REMAINING
---------- ---------- ---------- ---------- ---------- ---------- ----------
     34912 2014/07/28         33          1         32          0            
     34912 2014/08/04         32          1         31          0            
     34912 2014/08/11         30          2         29          0            
     34912 2014/08/25         30          1         28          0            
     34912 2014/09/01         29          0         28          0            
     34912 2014/09/15         28          1         27          0            
     34912 2014/09/22         27          0         27          0            
     34912 2014/09/29         26          1         26          0            
     34912 2014/10/06         26          2         24          0            
     34912 2014/10/13         24          0         24          0            
     34912 2014/10/20         23          1         23          0            
     34912 2014/11/03         23          2         21          0            
     34912 2014/12/01         21          1         20          0            
     34912 2014/12/08         20          2         18          0            
     34912 2015/01/05         18          0         18          0            
     34912 2015/02/02         17          0         18          0            
     34912 2015/02/09         14          1         17          0            
     34912 2015/02/16         14          2         15          0            
     34912 2015/02/23         11          1         14          0            
     34912 2015/03/02         11          4         10          0            
     34912 2015/03/09         11          2                     0         11 
     34912 2015/03/16          8          0                     1         11 
     34912 2015/03/23          8          0                     3         10 
     34912 2015/04/06          6          0                     2          7 
     34912 2015/04/20          4          0                     2          5 
     34912 2015/05/04          3          0                     1          3 
     34912 2015/06/22          2          0                     2          2 

 27 rows selected