create or replace PROCEDURE p_verdichte_pr_akt (p_user VARCHAR2)
AS
   LN_COUNT   NUMBER (8);
BEGIN
   /**********   Falls noetig,  Jahre in DT861 anlegen      ******************/
   INSERT INTO dt861 (DI023119,
                      DI023062,
                      DI023063,
                      DI023064,
                      DI023065,
                      DI023066,
                      DI023067,
                      DI023068,
                      DI023069,
                      DI023070,
                      DI023071,
                      DI023072)
      SELECT PR,
             jahr,
             SYSDATE,                                           -- Anlagedatum
             SYSDATE,                                        -- Ãnderungsdatum
             775,                                                      -- rwdu
             p_user,                                         -- Anlagebenutzer
             p_user,                                      -- Ãnderungsbenutzer
             NULL,                                                 -- Own.Liz.
             '',                                                        -- OSK
             775,                                                     -- Owner
             775,                                                     -- Group
             775                                                        -- Any
        FROM (SELECT *
                FROM (SELECT DISTINCT
                             DI001395 PR,
                             TO_NUMBER (EXTRACT (YEAR FROM di001519)) jahr
                        FROM dt472
                       WHERE DI001395 IN
                                (SELECT PROJEKT FROM T_PPMS_RISIKO_VERD)
                      UNION
                      SELECT DISTINCT di028836 pr, di028692 jahr
                        FROM DT871
                       WHERE DI028836 IN
                                (SELECT PROJEKT FROM T_PPMS_RISIKO_VERD)) SRC
               WHERE NOT EXISTS
                            (SELECT 1
                               FROM DT861 TGT
                              WHERE     src.PR = tgt.DI023119
                                    AND SRC.JAHR = TGT.DI023062));

   /*****************    Nutzen nach 861 uebertragen      *************/
    MERGE INTO DT861
    USING (
      SELECT COALESCE(SUM(COALESCE(di028706,0)),0) achieved_benefit, COALESCE(SUM(COALESCE(di028695,0)),0) planned_benefit, di028836 PR, di028692 YR
			FROM dt861 join dt871
      ON dt861.DI023119 = dt871.di028836 AND dt861.di023062 = dt871.di028692
      WHERE di023119 IN (SELECT PROJEKT FROM T_PPMS_RISIKO_VERD)
      GROUP BY di028836, di028692
      )
    ON (dt861.DI023119 = PR AND dt861.di023062 = YR)
    WHEN MATCHED THEN
    UPDATE SET DI028831 = COALESCE(planned_benefit,0), DI023173 = coalesce(achieved_benefit,0);

   /************   Kosten verdichten, falls VGR existieren      ****************/
   SELECT COUNT (*)
     INTO ln_count
     FROM dt466
    WHERE DI001390 IN (SELECT PROJEKT FROM T_PPMS_RISIKO_VERD);

   IF ln_count > 0
   THEN
      p_upd_kost_aufw_erl_akt ();
   END IF;

   -- Budget aus Baseline aktualisieren
   p_upd_budget_bl ();
   -- Risiken und Chancen verdichten
   p_verd_risiken_chancen_akt (p_user);
END p_verdichte_pr_akt;