--------------------------------------------------------
--  DDL for Procedure P_RunningSum_827
--------------------------------------------------------
CREATE OR REPLACE PROCEDURE "P_RUNNINGSUM_827" (DoComplete IN SIMPLE_INTEGER, ThisProjectID IN VARCHAR2, ThisReportID IN VARCHAR2)
IS
  -- Declarations:
  constProceeds CONSTANT VARCHAR2(2) := '8';
  maxLevel NUMBER := 0;
  i NUMBER := 0;
  
BEGIN
  -- initialize table DT827_CONDENSED
  IF DoComplete = 1 THEN
    BEGIN
      execute immediate 'TRUNCATE TABLE DT827_CONDENSED';
      commit;
    END;
  ELSE
    BEGIN
      execute immediate 'DELETE FROM DT827_CONDENSED WHERE MAINPROJECT = ' || ThisProjectID || ' AND REPORTID = ' || ThisReportID;
      commit;
    END;
  END IF;
  -- 
  -- TIMESLICE: at the moment only on a monthly basis, that means, unit is month - the "first of a month"-date represents the whole month (of the respective year).
  -- 
  IF DoComplete = 1 THEN
    BEGIN
      -- Insert VGR values FROM DT827 into table - costs and efforts per resource, revenues per proceeds
      INSERT INTO DT827_CONDENSED (DENSITYRATIO,MAINPROJECT,PROJECTLEVEL,PARENTPROJECT,PARENTTASK,PROJECTID,REPORTID,TASK,TRESOURCE,TIMESLICE,PC,PC_BL,AC,RC,TC,PE,PE_BL,AE,RE,PR,PR_BL,AR,RR)
      SELECT 1 DENSITYRATIO, -- VGR
        MAINPROJECT,PROJECTLEVEL,PARENTPROJECT,PARENTTASK,PROJECTID,REPORTID,TASK,TRESOURCE,TIMESLICE,
        SUM(COALESCE(PC*Costs,0)) PC,SUM(COALESCE(PC_BL*Costs,0)) PC_BL,SUM(COALESCE(AC*Costs,0)) AC,SUM(COALESCE(RC*Costs,0)) RC,SUM(COALESCE(TC*Costs,0)) TC,
        SUM(COALESCE(PE*Costs,0)) PE,SUM(COALESCE(PE_BL*Costs,0)) PE_BL,SUM(COALESCE(AE*Costs,0)) AE,SUM(COALESCE(RE*Costs,0)) RE,
        SUM(COALESCE(PR*Proceeds,0)) PR,SUM(COALESCE(PR_BL*Proceeds,0)) PR_BL,SUM(COALESCE(AR*Proceeds,0)) AR,SUM(COALESCE(RR*Proceeds,0)) RR
      FROM (
        SELECT p.DI001052 MAINPROJECT, pr.DI008603 PROJECTLEVEL, p.DI001003 PARENTPROJECT, p.DI001004 PARENTTASK, p.DI001001 PROJECTID, pr.DI008416 REPORTID,
          s.DI008517 TASK, s.DI008518 TRESOURCE, TRUNC(s.DI008523, 'MM') TIMESLICE,
          s.DI008661 PC,0.0 PC_BL,s.DI008663 AC,s.DI008665 RC,s.DI008666 TC,s.DI008660*r.DI001242 PE,0.0 PE_BL,s.DI008662*r.DI001242 AE,s.DI008664*r.DI001242 RE,
          s.DI008661 PR,0.0 PR_BL,s.DI008663 AR,s.DI008665 RR,
          CASE WHEN r.DI003414 = constProceeds THEN 0.0 ELSE 1.0 END Costs,CASE WHEN r.DI003414 = constProceeds THEN 1.0 ELSE 0.0 END Proceeds
        FROM DT461 p INNER JOIN DT823 pr ON p.DI001001 = pr.DI008413
          INNER JOIN DT827 s ON pr.DI008413 = s.DI008516 AND pr.DI008416 = s.DI008535 INNER JOIN DT467 r ON s.DI008518 = r.DI001218
        WHERE  pr.DI028505 = to_date('01.01.1970','DD.MM.RRRR') AND pr.DI008821 > to_date('01.01.1970','DD.MM.RRRR')
        UNION ALL
        SELECT p.DI001052 MAINPROJECT, pr.DI008603 PROJECTLEVEL, p.DI001003 PARENTPROJECT, p.DI001004 PARENTTASK, p.DI001001 PROJECTID, pr.DI008416 REPORTID,
          s.DI008517 TASK, s.DI008518 TRESOURCE, TRUNC(s.DI008523, 'MM') TIMESLICE,
          0.0 PC,s.DI008661 PC_BL,0.0 AC,0.0 RC,0.0 TC,0.0 PE,s.DI008660*r.DI001242 PE_BL,0.0 AE,0.0 RE,0.0 PR,s.DI008661 PR_BL,0.0 AR,0.0 RR,
          CASE WHEN r.DI003414 = constProceeds THEN 0.0 ELSE 1.0 END Costs,CASE WHEN r.DI003414 = constProceeds THEN 1.0 ELSE 0.0 END Proceeds
        FROM DT461 p INNER JOIN DT823 pr ON p.DI001001 = pr.DI008413 INNER JOIN DT823 bl ON pr.DI008578 = bl.DI008413 AND pr.DI008416 = bl.DI008416
          INNER JOIN DT827 s ON p.DI001001 = s.DI008516 AND bl.DI041716 = s.DI008535 INNER JOIN DT467 r ON s.DI008518 = r.DI001218
        WHERE  pr.DI028505 = to_date('01.01.1970','DD.MM.RRRR') AND pr.DI008821 > to_date('01.01.1970','DD.MM.RRRR')
      )
      GROUP BY 1,MAINPROJECT,PROJECTLEVEL,PARENTPROJECT,PARENTTASK,PROJECTID,REPORTID,TASK,TRESOURCE,TIMESLICE
      ;
      -- Insert VG values into table
      INSERT INTO DT827_CONDENSED (DENSITYRATIO,MAINPROJECT,PROJECTLEVEL,PARENTPROJECT,PARENTTASK,PROJECTID,REPORTID,TASK,TRESOURCE,TIMESLICE,PC,PC_BL,AC,RC,TC,PE,PE_BL,AE,RE,PR,PR_BL,AR,RR)
      SELECT 2 DENSITYRATIO, -- VG
        MAINPROJECT,PROJECTLEVEL,PARENTPROJECT,PARENTTASK,PROJECTID,REPORTID,TASK,'-' TRESOURCE,TIMESLICE,
        SUM(PC) PC,SUM(PC_BL) PC_BL,SUM(AC) AC,SUM(RC) RC,SUM(TC) TC,SUM(PE) PE,SUM(PE_BL) PE_BL,SUM(AE) AE,SUM(RE) RE,SUM(PR) PR,SUM(PR_BL) PR_BL,SUM(AR) AR,SUM(RR) RR
      FROM DT827_CONDENSED
      GROUP BY MAINPROJECT,PROJECTLEVEL,PARENTPROJECT,PARENTTASK,PROJECTID,REPORTID,TASK,TIMESLICE
      ;
      -- Insert aggregated subproject values mapped to parent task into table
      SELECT MAX(COALESCE(PROJECTLEVEL,0)) INTO maxLevel FROM DT827_CONDENSED WHERE DENSITYRATIO = 2;
      FOR i IN REVERSE 1..maxLevel LOOP
        INSERT INTO DT827_CONDENSED (DENSITYRATIO,MAINPROJECT,PROJECTLEVEL,PARENTPROJECT,PARENTTASK,PROJECTID,REPORTID,TASK,TRESOURCE,TIMESLICE,PC,PC_BL,AC,RC,TC,PE,PE_BL,AE,RE,PR,PR_BL,AR,RR)
        SELECT 2 DENSITYRATIO, -- VG
          t.MAINPROJECT,s.PROJECTLEVEL-1 PROJECTLEVEL,t.PARENTPROJECT,t.PARENTTASK,t.PROJECTID,s.REPORTID,s.PARENTTASK TASK,s.TRESOURCE,s.TIMESLICE,
          s.PC,s.PC_BL,s.AC,s.RC,s.TC,s.PE,s.PE_BL,s.AE,s.RE,s.PR,s.PR_BL,s.AR,s.RR
        FROM (
          SELECT MAINPROJECT,PROJECTLEVEL,PARENTPROJECT,PARENTTASK,PROJECTID,REPORTID,'-' TASK,'-' TRESOURCE,TIMESLICE,
            SUM(PC) PC,SUM(PC_BL) PC_BL,SUM(AC) AC,SUM(RC) RC,SUM(TC) TC,SUM(PE) PE,SUM(PE_BL) PE_BL,SUM(AE) AE,SUM(RE) RE,SUM(PR) PR,SUM(PR_BL) PR_BL,SUM(AR) AR,SUM(RR) RR
          FROM DT827_CONDENSED
          WHERE DENSITYRATIO = 2 AND PROJECTLEVEL = i AND PARENTPROJECT IS NOT NULL AND PARENTTASK IS NOT NULL
          GROUP BY MAINPROJECT,PROJECTLEVEL,PARENTPROJECT,PARENTTASK,PROJECTID,REPORTID,TIMESLICE
        ) s INNER JOIN (
          SELECT DI001052 MAINPROJECT,DI001003 PARENTPROJECT,DI001004 PARENTTASK,DI001001 PROJECTID
          FROM DT461
        ) t ON s.PARENTPROJECT = t.PROJECTID
        ;
      END LOOP;
      -- Insert PR values into table
      INSERT INTO DT827_CONDENSED (DENSITYRATIO,MAINPROJECT,PROJECTLEVEL,PARENTPROJECT,PARENTTASK,PROJECTID,REPORTID,TASK,TRESOURCE,TIMESLICE,PC,PC_BL,AC,RC,TC,PE,PE_BL,AE,RE,PR,PR_BL,AR,RR)
      SELECT 3 DENSITYRATIO, -- PR
        MAINPROJECT,PROJECTLEVEL,PARENTPROJECT,PARENTTASK,PROJECTID,REPORTID,'-' TASK,'-' TRESOURCE,TIMESLICE,
        SUM(PC) PC,SUM(PC_BL) PC_BL,SUM(AC) AC,SUM(RC) RC,SUM(TC) TC,SUM(PE) PE,SUM(PE_BL) PE_BL,SUM(AE) AE,SUM(RE) RE,SUM(PR) PR,SUM(PR_BL) PR_BL,SUM(AR) AR,SUM(RR) RR
      FROM DT827_CONDENSED
      WHERE DENSITYRATIO = 2
      GROUP BY MAINPROJECT,PROJECTLEVEL,PARENTPROJECT,PARENTTASK,PROJECTID,REPORTID,TIMESLICE
      ;
      -- Compute the cumulated values
      UPDATE DT827_CONDENSED t SET 
        (CML_PC,CML_PC_BL,CML_AC,CML_RC,CML_TC,CML_PE,CML_PE_BL,CML_AE,CML_RE,CML_PR,CML_PR_BL,CML_AR,CML_RR) = (
          SELECT SUM(PC) CML_PC,SUM(PC_BL) CML_PC_BL,SUM(AC) CML_AC,SUM(RC) CML_RC,SUM(TC) CML_TC,
            SUM(PE) CML_PE,SUM(PE_BL) CML_PE_BL,SUM(AE) CML_AE,SUM(RE) CML_RE,SUM(PR) CML_PR,SUM(PR_BL) CML_PR_BL,SUM(AR) CML_AR,SUM(RR) CML_RR
          FROM DT827_CONDENSED s WHERE s.DENSITYRATIO = t.DENSITYRATIO AND s.PROJECTID = t.PROJECTID AND s.REPORTID = t.REPORTID AND 
          t.TASK = s.TASK AND t.TRESOURCE = s.TRESOURCE AND t.TIMESLICE >= s.TIMESLICE)
      ;
      -- Compute EVA values
      UPDATE DT827_CONDENSED t SET 
        (PROGRESS,EV) = (
          SELECT PLANTA_CALC_PRORESS(MAX(s.CML_TC),t.CML_AC)*100 PROGRESS,PLANTA_CALC_PRORESS(MAX(s.CML_TC),t.CML_AC)*MAX(s.CML_PC_BL) EV
          FROM DT827_CONDENSED s 
          WHERE s.DENSITYRATIO = t.DENSITYRATIO AND s.PROJECTID = t.PROJECTID AND s.REPORTID = t.REPORTID AND t.TASK = s.TASK AND t.TRESOURCE = s.TRESOURCE)
      ;
      UPDATE DT827_CONDENSED SET 
        SV = EV - CML_PC_BL,
        CV = EV - CML_AC,
        SPI = CASE WHEN CML_PC_BL = 0 THEN 0 ELSE EV / CML_PC_BL END,
        CPI = CASE WHEN CML_AC = 0 THEN 0 ELSE EV / CML_AC END
      ;
      commit;
    END;
  ELSE
    BEGIN
      -- Insert VGR values FROM DT827 into table - costs and efforts per resource, revenues per proceeds
      INSERT INTO DT827_CONDENSED (DENSITYRATIO,MAINPROJECT,PROJECTLEVEL,PARENTPROJECT,PARENTTASK,PROJECTID,REPORTID,TASK,TRESOURCE,TIMESLICE,PC,PC_BL,AC,RC,TC,PE,PE_BL,AE,RE,PR,PR_BL,AR,RR)
      SELECT 1 DENSITYRATIO, -- VGR
        MAINPROJECT,PROJECTLEVEL,PARENTPROJECT,PARENTTASK,PROJECTID,REPORTID,TASK,TRESOURCE,TIMESLICE,
        SUM(COALESCE(PC*Costs,0)) PC,SUM(COALESCE(PC_BL*Costs,0)) PC_BL,SUM(COALESCE(AC*Costs,0)) AC,SUM(COALESCE(RC*Costs,0)) RC,SUM(COALESCE(TC*Costs,0)) TC,
        SUM(COALESCE(PE*Costs,0)) PE,SUM(COALESCE(PE_BL*Costs,0)) PE_BL,SUM(COALESCE(AE*Costs,0)) AE,SUM(COALESCE(RE*Costs,0)) RE,
        SUM(COALESCE(PR*Proceeds,0)) PR,SUM(COALESCE(PR_BL*Proceeds,0)) PR_BL,SUM(COALESCE(AR*Proceeds,0)) AR,SUM(COALESCE(RR*Proceeds,0)) RR
      FROM (
        SELECT p.DI001052 MAINPROJECT, pr.DI008603 PROJECTLEVEL, p.DI001003 PARENTPROJECT, p.DI001004 PARENTTASK, p.DI001001 PROJECTID, pr.DI008416 REPORTID,
          s.DI008517 TASK, s.DI008518 TRESOURCE, TRUNC(s.DI008523, 'MM') TIMESLICE,
          s.DI008661 PC,0.0 PC_BL,s.DI008663 AC,s.DI008665 RC,s.DI008666 TC,s.DI008660*r.DI001242 PE,0.0 PE_BL,s.DI008662*r.DI001242 AE,s.DI008664*r.DI001242 RE,
          s.DI008661 PR,0.0 PR_BL,s.DI008663 AR,s.DI008665 RR,
          CASE WHEN r.DI003414 = constProceeds THEN 0.0 ELSE 1.0 END Costs,CASE WHEN r.DI003414 = constProceeds THEN 1.0 ELSE 0.0 END Proceeds
        FROM DT461 p INNER JOIN DT823 pr ON p.DI001001 = pr.DI008413
          INNER JOIN DT827 s ON pr.DI008413 = s.DI008516 AND pr.DI008416 = s.DI008535 INNER JOIN DT467 r ON s.DI008518 = r.DI001218
        WHERE p.DI001052 = ThisProjectID AND pr.DI008416 = ThisReportID AND pr.DI028505 = to_date('01.01.1970','DD.MM.RRRR') AND pr.DI008821 > to_date('01.01.1970','DD.MM.RRRR')
        UNION ALL
        SELECT p.DI001052 MAINPROJECT, pr.DI008603 PROJECTLEVEL, p.DI001003 PARENTPROJECT, p.DI001004 PARENTTASK, p.DI001001 PROJECTID, pr.DI008416 REPORTID,
          s.DI008517 TASK, s.DI008518 TRESOURCE, TRUNC(s.DI008523, 'MM') TIMESLICE,
          0.0 PC,s.DI008661 PC_BL,0.0 AC,0.0 RC,0.0 TC,0.0 PE,s.DI008660*r.DI001242 PE_BL,0.0 AE,0.0 RE,0.0 PR,s.DI008661 PR_BL,0.0 AR,0.0 RR,
          CASE WHEN r.DI003414 = constProceeds THEN 0.0 ELSE 1.0 END Costs,CASE WHEN r.DI003414 = constProceeds THEN 1.0 ELSE 0.0 END Proceeds
        FROM DT461 p INNER JOIN DT823 pr ON p.DI001001 = pr.DI008413 INNER JOIN DT823 bl ON pr.DI008578 = bl.DI008413 AND pr.DI008416 = bl.DI008416
          INNER JOIN DT827 s ON p.DI001001 = s.DI008516 AND bl.DI041716 = s.DI008535 INNER JOIN DT467 r ON s.DI008518 = r.DI001218
        WHERE p.DI001052 = ThisProjectID AND pr.DI008416 = ThisReportID AND pr.DI028505 = to_date('01.01.1970','DD.MM.RRRR') AND pr.DI008821 > to_date('01.01.1970','DD.MM.RRRR')
      )
      GROUP BY 1,MAINPROJECT,PROJECTLEVEL,PARENTPROJECT,PARENTTASK,PROJECTID,REPORTID,TASK,TRESOURCE,TIMESLICE
      ;
      -- Insert VG values into table
      INSERT INTO DT827_CONDENSED (DENSITYRATIO,MAINPROJECT,PROJECTLEVEL,PARENTPROJECT,PARENTTASK,PROJECTID,REPORTID,TASK,TRESOURCE,TIMESLICE,PC,PC_BL,AC,RC,TC,PE,PE_BL,AE,RE,PR,PR_BL,AR,RR)
      SELECT 2 DENSITYRATIO, -- VG
        MAINPROJECT,PROJECTLEVEL,PARENTPROJECT,PARENTTASK,PROJECTID,REPORTID,TASK,'-' TRESOURCE,TIMESLICE,
        SUM(PC) PC,SUM(PC_BL) PC_BL,SUM(AC) AC,SUM(RC) RC,SUM(TC) TC,SUM(PE) PE,SUM(PE_BL) PE_BL,SUM(AE) AE,SUM(RE) RE,SUM(PR) PR,SUM(PR_BL) PR_BL,SUM(AR) AR,SUM(RR) RR
      FROM DT827_CONDENSED
      WHERE MAINPROJECT = ThisProjectID AND REPORTID = ThisReportID
      GROUP BY MAINPROJECT,PROJECTLEVEL,PARENTPROJECT,PARENTTASK,PROJECTID,REPORTID,TASK,TIMESLICE
      ;
      -- Insert aggregated subproject values mapped to parent TASK into table
      SELECT MAX(COALESCE(PROJECTLEVEL,0)) INTO maxLevel FROM DT827_CONDENSED WHERE DENSITYRATIO = 2 AND MAINPROJECT = ThisProjectID AND REPORTID = ThisReportID;
      FOR i IN REVERSE 1..maxLevel LOOP
        INSERT INTO DT827_CONDENSED (DENSITYRATIO,MAINPROJECT,PROJECTLEVEL,PARENTPROJECT,PARENTTASK,PROJECTID,REPORTID,TASK,TRESOURCE,TIMESLICE,PC,PC_BL,AC,RC,TC,PE,PE_BL,AE,RE,PR,PR_BL,AR,RR)
        SELECT 2 DENSITYRATIO, -- VG
          t.MAINPROJECT,s.PROJECTLEVEL-1 PROJECTLEVEL,t.PARENTPROJECT,t.PARENTTASK,t.PROJECTID,s.REPORTID,s.PARENTTASK TASK,s.TRESOURCE,s.TIMESLICE,
          s.PC,s.PC_BL,s.AC,s.RC,s.TC,s.PE,s.PE_BL,s.AE,s.RE,s.PR,s.PR_BL,s.AR,s.RR
        FROM (
          SELECT MAINPROJECT,PROJECTLEVEL,PARENTPROJECT,PARENTTASK,PROJECTID,REPORTID,'-' TASK,'-' TRESOURCE,TIMESLICE,
            SUM(PC) PC,SUM(PC_BL) PC_BL,SUM(AC) AC,SUM(RC) RC,SUM(TC) TC,SUM(PE) PE,SUM(PE_BL) PE_BL,SUM(AE) AE,SUM(RE) RE,SUM(PR) PR,SUM(PR_BL) PR_BL,SUM(AR) AR,SUM(RR) RR
          FROM DT827_CONDENSED
          WHERE DENSITYRATIO = 2 AND PROJECTLEVEL = i AND PARENTPROJECT IS NOT NULL AND PARENTTASK IS NOT NULL AND 
            MAINPROJECT = ThisProjectID AND REPORTID = ThisReportID
          GROUP BY MAINPROJECT,PROJECTLEVEL,PARENTPROJECT,PARENTTASK,PROJECTID,REPORTID,TIMESLICE
        ) s INNER JOIN (
          SELECT DISTINCT DI001052 MAINPROJECT,DI001003 PARENTPROJECT,DI001004 PARENTTASK,DI001001 PROJECTID
          FROM DT461
          WHERE DI001052 = ThisProjectID
        ) t ON s.PARENTPROJECT = t.PROJECTID
        ;
      END LOOP;
      -- Insert PR values into table
      INSERT INTO DT827_CONDENSED (DENSITYRATIO,MAINPROJECT,PROJECTLEVEL,PARENTPROJECT,PARENTTASK,PROJECTID,REPORTID,TASK,TRESOURCE,TIMESLICE,PC,PC_BL,AC,RC,TC,PE,PE_BL,AE,RE,PR,PR_BL,AR,RR)
      SELECT 3 DENSITYRATIO, -- PR
        MAINPROJECT,PROJECTLEVEL,PARENTPROJECT,PARENTTASK,PROJECTID,REPORTID,'-' TASK,'-' TRESOURCE,TIMESLICE,
        SUM(PC) PC,SUM(PC_BL) PC_BL,SUM(AC) AC,SUM(RC) RC,SUM(TC) TC,SUM(PE) PE,SUM(PE_BL) PE_BL,SUM(AE) AE,SUM(RE) RE,SUM(PR) PR,SUM(PR_BL) PR_BL,SUM(AR) AR,SUM(RR) RR
      FROM DT827_CONDENSED
      WHERE DENSITYRATIO = 2 AND MAINPROJECT = ThisProjectID AND REPORTID = ThisReportID
      GROUP BY MAINPROJECT,PROJECTLEVEL,PARENTPROJECT,PARENTTASK,PROJECTID,REPORTID,TIMESLICE
      ;
      -- Compute the cumulated values
      UPDATE DT827_CONDENSED t SET 
        (CML_PC,CML_PC_BL,CML_AC,CML_RC,CML_TC,CML_PE,CML_PE_BL,CML_AE,CML_RE,CML_PR,CML_PR_BL,CML_AR,CML_RR) = (
          SELECT SUM(PC) CML_PC,SUM(PC_BL) CML_PC_BL,SUM(AC) CML_AC,SUM(RC) CML_RC,SUM(TC) CML_TC,
            SUM(PE) CML_PE,SUM(PE_BL) CML_PE_BL,SUM(AE) CML_AE,SUM(RE) CML_RE,SUM(PR) CML_PR,SUM(PR_BL) CML_PR_BL,SUM(AR) CML_AR,SUM(RR) CML_RR
          FROM DT827_CONDENSED s WHERE s.DENSITYRATIO = t.DENSITYRATIO AND s.PROJECTID = t.PROJECTID AND s.REPORTID = t.REPORTID AND 
            t.TASK = s.TASK AND t.TRESOURCE = s.TRESOURCE AND t.TIMESLICE >= s.TIMESLICE)
      WHERE MAINPROJECT = ThisProjectID AND REPORTID = ThisReportID
      ;
      -- Compute EVA values
      UPDATE DT827_CONDENSED t SET 
        (PROGRESS,EV) = (
          SELECT PLANTA_CALC_PRORESS(MAX(s.CML_TC),t.CML_AC)*100 PROGRESS,PLANTA_CALC_PRORESS(MAX(s.CML_TC),t.CML_AC)*MAX(s.CML_PC_BL) EV
          FROM DT827_CONDENSED s 
          WHERE s.DENSITYRATIO = t.DENSITYRATIO AND s.PROJECTID = t.PROJECTID AND s.REPORTID = t.REPORTID AND t.TASK = s.TASK AND t.TRESOURCE = s.TRESOURCE)
      WHERE t.MAINPROJECT = ThisProjectID AND t.REPORTID = ThisReportID
      ;
      UPDATE DT827_CONDENSED SET 
        SV = EV - CML_PC_BL,
        CV = EV - CML_AC,
        SPI = CASE WHEN CML_PC_BL = 0 THEN 0 ELSE EV / CML_PC_BL END,
        CPI = CASE WHEN CML_AC = 0 THEN 0 ELSE EV / CML_AC END
      WHERE MAINPROJECT = ThisProjectID AND REPORTID = ThisReportID
      ;
      commit;
    END;
  END IF;
  -- DEBUG: SELECT * FROM DT827_CONDENSED;
END;