ALTER TABLE DETAPURACONTR ADD VRAJUSTECREDSAIDDI FLOAT(126)
/
ALTER TABLE DETAPURACONTR ADD VRAJUSTEDEBSAIDDI FLOAT(126)
/
ALTER TABLE DETAPURACONTR ADD VRAJUSTECREDSAIDDB FLOAT(126)
/
ALTER TABLE DETAPURACONTR ADD VRAJUSTEDEBSAIDDB FLOAT(126)
/
BEGIN
    FOR C_DET IN (
        SELECT
            NRSEQUMAPAPU,
            SUM(VRAJUSTECREDENTR) AS VRAJUSTECREDENTR,
            SUM(VRAJUSTEDEBENTR) AS VRAJUSTEDEBENTR,
            SUM(VRAJUSTECREDSAIDDI + VRAJUSTECREDSAIDDB) AS VRAJUSTECREDSAID,
            SUM(VRAJUSTEDEBSAIDDI + VRAJUSTEDEBSAIDDB) AS VRAJUSTEDEBSAID,
            SUM(VRAJUSTECREDSAIDDI) AS VRAJUSTECREDSAIDDI,
            SUM(VRAJUSTEDEBSAIDDI) AS VRAJUSTEDEBSAIDDI,
            SUM(VRAJUSTECREDSAIDDB) AS VRAJUSTECREDSAIDDB,
            SUM(VRAJUSTEDEBSAIDDB) AS VRAJUSTEDEBSAIDDB
        FROM
            (
                SELECT
                    NRSEQUMAPAPU,
                    NVL(SUM(DECODE(IDTPAJUSTE,'1',VRAJUSTE,0) ),0) AS VRAJUSTECREDENTR,
                    NVL(SUM(DECODE(IDTPAJUSTE,'0',VRAJUSTE,0) ),0) AS VRAJUSTEDEBENTR,
                    0 AS VRAJUSTECREDSAIDDI,
                    0 AS VRAJUSTEDEBSAIDDI,
                    0 AS VRAJUSTECREDSAIDDB,
                    0 AS VRAJUSTEDEBSAIDDB
                FROM
                    AJUSTEAPURCONTR
                WHERE
                    IDINDICAJUSTE = 'C'
                GROUP BY
                    NRSEQUMAPAPU
                UNION ALL
                SELECT
                    NRSEQUMAPAPU,
                    0 AS VRAJUSTECREDENTR,
                    0 AS VRAJUSTEDEBENTR,
                    NVL(SUM(DECODE(IDTPAJUSTE,'1',VRAJUSTE,0) ),0) AS VRAJUSTECREDSAIDDI,
                    NVL(SUM(DECODE(IDTPAJUSTE,'0',VRAJUSTE,0) ),0) AS VRAJUSTEDEBSAIDDI,
                    0 AS VRAJUSTECREDSAIDDB,
                    0 AS VRAJUSTEDEBSAIDDB
                FROM
                    AJUSTEAPURCONTR
                WHERE
                    IDINDICAJUSTE = 'DI'
                GROUP BY
                    NRSEQUMAPAPU
                UNION ALL
                SELECT
                    NRSEQUMAPAPU,
                    0 AS VRAJUSTECREDENTR,
                    0 AS VRAJUSTEDEBENTR,
                    0 AS VRAJUSTECREDSAIDDI,
                    0 AS VRAJUSTEDEBSAIDDI,
                    NVL(SUM(DECODE(IDTPAJUSTE,'1',VRAJUSTE,0) ),0) AS VRAJUSTECREDSAIDDB,
                    NVL(SUM(DECODE(IDTPAJUSTE,'0',VRAJUSTE,0) ),0) AS VRAJUSTEDEBSAIDDB
                FROM
                    AJUSTEAPURCONTR
                WHERE
                    IDINDICAJUSTE = 'DB'
                GROUP BY
                    NRSEQUMAPAPU
            )
        GROUP BY
            NRSEQUMAPAPU
    ) LOOP
        UPDATE DETAPURACONTR
        SET
            VRAJUSTECREDSAID = C_DET.VRAJUSTECREDSAID,
            VRAJUSTEDEBSAID = C_DET.VRAJUSTEDEBSAID,
            VRAJUSTECREDSAIDDI = C_DET.VRAJUSTECREDSAIDDI,
            VRAJUSTEDEBSAIDDI = C_DET.VRAJUSTEDEBSAIDDI,
            VRAJUSTECREDSAIDDB = C_DET.VRAJUSTECREDSAIDDB,
            VRAJUSTEDEBSAIDDB = C_DET.VRAJUSTEDEBSAIDDB
        WHERE
            NRSEQUMAPAPU = C_DET.NRSEQUMAPAPU;
    END LOOP;
END;
/
COMMIT