MATH HEAVY - SQL and PLSQL - PROCEDURE/FUNCTION

Hello

I have a chart with lines more million. In this table, I have a lot of members associated with 60-70 facility_names.
Unique index is on CMPNY, FNAME, M_ID MONTHS.

Below, I have some EXAMPLES OF 3 facilities, ADIOS DAOMATO, NADA CAPETOWN and NADA NEW DIAMONDS.

These companies have two different contract.

For example, NADA CAPETOWN & NADA NEW DIAMOND
-As soon as total Amount_1 CDA for a member associated with above facilities will more $2,000 and total Amount_2 passes over $4000, installation gets insurance protection for 90% of the sum, he surpassed in each categories.

In example below, M0001 of NADA CAPETOWN, CDA AMOUNT_1 total more than $2,000 to 1 October 2009 (total $2160), installation gets recoveries of $144 ((2160-2000) * 90%).
Similarly, in November 1, 2009, total of CDA is $2180 and installation gets recoveries of $18 ((2180-2160) * 90%). I subtracted $2160 because installation has already received $144 for having exceeded $160, more than 2000 back on October 1, 2009.

Very similar, total if Amount_2 CDA exceeds 4000, TI $ receives 90% of the amount after $4000. (the calculations are the same as above)
The column of recoveries should summarize recoveries exceed amount_1 and amount_2 per month.

During this time,.

ADIOS DAOMATO has a different contract. When single member of this facility (CDA AMOUNT_1 + AMOUNT_2 CDA) is greater than 5000, the plant recovers 90%.

In the case of sum CDA Member Unique of ADIOS DAOMATO of AMOUNT_1 & AMOUNT_2 (combined) will more than $5,000, then installation gets insurance protection for 90% of the amount it exceeded.
In the example below, total ADIOS DAOMATO CDA exceeds $5,000 in October 1, 2009 by $540 ($11080 total YTD), so installation retrieves ((11080-10000$) * 90%) = $972. After months of November 1, 2009, $40 ($20 + $20) added more than $5,000, so installation recoveres (($5580-5540 $) * 90%) = $36...


* I don't mind saw these two distinct processes. Please note that the calculation of recoveries ($) are by a single member of the installation by month/year, but the result should be to ease a month. So far I have annually. But I really need every month for financials.*

Purpose: I need to know what level of insurance protection the installation receives monthly.
For the duration of the contract 1 the Reisurance Total must be entered in the column "RECOVERIES" (COLLECTIONS of REINSURANCE OF AMOUNT_1 + the RECOVERIES of REINSURANCE OF AMOUNT_2).

For the duration of the contract 2, there is only 1 recovery mode.

As the I mentioned, that's a CALCULATION for a YEAR when the year changes, everything resets all members. *
_ I would greatly appreciate your help. _

Using oracle 10g
DROP TABLE PAYMENT_1;

create table PAYMENT_1
(CMPNY      VARCHAR2(4),
 F_NAME          VARCHAR2(100),
 M_ID               VARCHAR2(8),
 MONTH         DATE,
 AMOUNT1    FLOAT(126),
 AMOUNT2    FLOAT(126)
 );
 
 INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2) VALUES ('C001','NADA CAPETOWN', 'M0002','01-JAN-2009','20','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2) VALUES ('C001','NADA CAPETOWN', 'M0002','01-JAN-2009','90','180');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN', 'M0002','01-JAN-2009','100','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN', 'M0002','01-FEB-2009','20','20');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN', 'M0002','01-FEB-2009','20','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN', 'M0002','01-JUL-2009','1000','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN', 'M0002','01-AUG-2009','500','2000');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2) VALUES ('C001','NADA CAPETOWN', 'M0002','01-AUG-2009','20','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2) VALUES ('C001','NADA CAPETOWN', 'M0002','01-OCT-2009','90','180');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN', 'M0002','01-OCT-2009','100','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN', 'M0002','01-NOV-2009','20','20');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN', 'M0002','01-DEC-2009','20','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN', 'M0002','01-DEC-2009','1000','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN', 'M0002','01-DEC-2009','500','2000');


INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2) VALUES ('C001','NADA CAPETOWN', 'M0001','01-JAN-2009','20','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2) VALUES ('C001','NADA CAPETOWN', 'M0001','01-JAN-2009','90','180');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN', 'M0001','01-JAN-2009','100','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN', 'M0001','01-FEB-2009','20','20');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN', 'M0001','01-FEB-2009','20','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN', 'M0001','01-JUL-2009','1200','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN', 'M0001','01-AUG-2009','500','2200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2) VALUES ('C001','NADA CAPETOWN', 'M0001','01-AUG-2009','20','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2) VALUES ('C001','NADA CAPETOWN', 'M0001','01-OCT-2009','90','180');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN', 'M0001','01-OCT-2009','100','220');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN', 'M0001','01-NOV-2009','20','20');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN', 'M0001','01-DEC-2009','20','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN', 'M0001','01-DEC-2009','1100','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN', 'M0001','01-DEC-2009','500','2200');


INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2) VALUES ('C001','NADA NEW DIAMOND','M0003','01-JAN-2009','20','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2) VALUES ('C001','NADA NEW DIAMOND','M0003','01-JAN-2009','90','180');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA NEW DIAMOND','M0003','01-JAN-2009','100','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA NEW DIAMOND','M0003','01-FEB-2009','20','20');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA NEW DIAMOND','M0003','01-FEB-2009','20','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA NEW DIAMOND','M0003','01-JUL-2009','1000','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA NEW DIAMOND','M0003','01-AUG-2009','500','2000');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2) VALUES ('C001','NADA NEW DIAMOND','M0003','01-AUG-2009','20','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2) VALUES ('C001','NADA NEW DIAMOND','M0003','01-OCT-2009','90','180');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA NEW DIAMOND','M0003','01-OCT-2009','100','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA NEW DIAMOND','M0003','01-NOV-2009','20','20');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA NEW DIAMOND','M0003','01-DEC-2009','20','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA NEW DIAMOND','M0003','01-DEC-2009','1000','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA NEW DIAMOND','M0003','01-DEC-2009','500','2000');

INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2) VALUES ('C001','NADA CAPETOWN','M0004','01-JAN-2010','20','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2) VALUES ('C001','NADA CAPETOWN','M0004','01-JAN-2010','90','180');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN','M0004','01-JAN-2010','100','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN','M0004','01-FEB-2010','20','20');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN','M0004','01-FEB-2010','20','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN','M0004','01-JUL-2010','1000','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN','M0004','01-AUG-2010','500','2000');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2) VALUES ('C001','NADA CAPETOWN','M0004','01-AUG-2010','20','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2) VALUES ('C001','NADA CAPETOWN','M0004','01-OCT-2010','90','180');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN','M0004','01-OCT-2010','100','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN','M0004','01-NOV-2010','20','20');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN','M0004','01-DEC-2010','20','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN','M0004','01-DEC-2010','1000','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA CAPETOWN','M0004','01-DEC-2010','500','2000');

INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2) VALUES ('C001','NADA NEW DIAMOND','M0003','01-JAN-2010','20','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2) VALUES ('C001','NADA NEW DIAMOND','M0003','01-JAN-2010','90','180');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA NEW DIAMOND','M0003','01-JAN-2010','100','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA NEW DIAMOND','M0003','01-FEB-2010','20','20');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA NEW DIAMOND','M0003','01-FEB-2010','20','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA NEW DIAMOND','M0003','01-JUL-2010','1000','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA NEW DIAMOND','M0003','01-AUG-2010','500','2000');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2) VALUES ('C001','NADA NEW DIAMOND','M0003','01-AUG-2010','20','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2) VALUES ('C001','NADA NEW DIAMOND','M0003','01-OCT-2010','90','180');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA NEW DIAMOND','M0003','01-OCT-2010','100','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA NEW DIAMOND','M0003','01-NOV-2010','20','20');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA NEW DIAMOND','M0003','01-DEC-2010','20','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA NEW DIAMOND','M0003','01-DEC-2010','1000','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','NADA NEW DIAMOND','M0003','01-DEC-2010','500','2000');


INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2) VALUES ('C001','ADIOS DAOMATO','M0010','01-JAN-2009','20','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2) VALUES ('C001','ADIOS DAOMATO','M0010','01-JAN-2009','90','180');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','ADIOS DAOMATO','M0010','01-JAN-2009','100','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','ADIOS DAOMATO','M0010','01-FEB-2009','20','20');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','ADIOS DAOMATO','M0010','01-FEB-2009','20','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','ADIOS DAOMATO','M0010','01-JUL-2009','1000','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','ADIOS DAOMATO','M0010','01-AUG-2009','500','2000');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2) VALUES ('C001','ADIOS DAOMATO','M0010','01-AUG-2009','20','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2) VALUES ('C001','ADIOS DAOMATO','M0010','01-OCT-2009','90','180');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','ADIOS DAOMATO','M0010','01-OCT-2009','100','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','ADIOS DAOMATO','M0010','01-NOV-2009','20','20');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','ADIOS DAOMATO','M0010','01-DEC-2009','20','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','ADIOS DAOMATO','M0010','01-DEC-2009','1000','200');
INSERT INTO PAYMENT_1 (CMPNY, F_NAME, M_ID, MONTH, AMOUNT1, AMOUNT2)VALUES ('C001','ADIOS DAOMATO','M0010','01-DEC-2009','500','2000');

commit;


--RESULTS SHOULD BE SOMETHING LIKE THE FOLLOWING

DROP TABLE PAYMENT_2;

create table PAYMENT_2
(CMPNY      VARCHAR2(4),
 F_NAME          VARCHAR2(100),
  RECOVERIES     FLOAT(126),
 MONTH         DATE
 );
 


INSERT INTO PAYMENT_2 (CMPNY, F_NAME, RECOVERIES, MONTH) VALUES ('C001','ADIOS DAOMATO','486','01-OCT-2009');
INSERT INTO PAYMENT_2 (CMPNY, F_NAME, RECOVERIES, MONTH) VALUES ('C001','ADIOS DAOMATO','36','01-NOV-2009');
INSERT INTO PAYMENT_2 (CMPNY, F_NAME, RECOVERIES, MONTH) VALUES ('C001','ADIOS DAOMATO','3528','01-DEC-2009');

INSERT INTO PAYMENT_2 (CMPNY, F_NAME, RECOVERIES, MONTH) VALUES ('C001','NADA CAPETOWN','144','01-OCT-2009');
INSERT INTO PAYMENT_2 (CMPNY, F_NAME, RECOVERIES, MONTH) VALUES ('C001','NADA CAPETOWN','18','01-NOV-2009');
INSERT INTO PAYMENT_2 (CMPNY, F_NAME, RECOVERIES, MONTH) VALUES ('C001','NADA CAPETOWN','6786','01-DEC-2009');
INSERT INTO PAYMENT_2 (CMPNY, F_NAME, RECOVERIES, MONTH) VALUES ('C001','NADA CAPETOWN','3150','01-DEC-2010');

INSERT INTO PAYMENT_2 (CMPNY, F_NAME, RECOVERIES, MONTH) VALUES ('C001','NADA NEW DIAMOND','2150','01-DEC-2009');
INSERT INTO PAYMENT_2 (CMPNY, F_NAME, RECOVERIES, MONTH) VALUES ('C001','NADA NEW DIAMOND','3150','01-DEC-2010');

commit;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

I think this can do:

SELECT cmpny
     , f_name
     , month
     , SUM(recoveries)
FROM
(
        SELECT cmpny
             , f_name
             , m_id
             , month
             , ytd_amount1
             , ytd_amount2
             , ytd_amount
             , CASE
                   WHEN f_name IN ('NADA CAPETOWN','NADA NEW DIAMOND')
                   THEN
                       CASE
                           WHEN ytd_amount1 > 2000
                           THEN .9*(ytd_amount1 - GREATEST(2000,LAG(ytd_amount1) OVER (PARTITION BY cmpny, f_name, m_id ORDER BY month)))
                           ELSE 0
                       END
                     + CASE
                           WHEN ytd_amount2 > 4000
                           THEN .9*(ytd_amount2 - GREATEST(4000,LAG(ytd_amount2) OVER (PARTITION BY cmpny, f_name, m_id ORDER BY month)))
                           ELSE 0
                       END
                   WHEN f_name IN ('ADIOS DAOMATO')
                   THEN
                       CASE
                           WHEN ytd_amount > 5000
                           THEN .9*(ytd_amount - GREATEST(5000,LAG(ytd_amount) OVER (PARTITION BY cmpny, f_name, m_id ORDER BY month)))
                           ELSE 0
                       END
               END AS recoveries
        FROM
        (
                SELECT cmpny
                     , f_name
                     , m_id
                     , month
                     , SUM(amount1_per_month) OVER (PARTITION BY cmpny, f_name, TRUNC(month,'YEAR'), m_id ORDER BY month) AS ytd_amount1
                     , SUM(amount2_per_month) OVER (PARTITION BY cmpny, f_name, TRUNC(month,'YEAR'), m_id ORDER BY month) AS ytd_amount2
                     , SUM(amount_per_month) OVER (PARTITION BY cmpny, f_name, TRUNC(month,'YEAR'), m_id ORDER BY month) AS ytd_amount
                FROM
                (
                        SELECT cmpny
                             , f_name
                             , m_id
                             , month
                             , SUM(amount1) amount1_per_month
                             , SUM(amount2) amount2_per_month
                             , SUM(amount1 + amount2) amount_per_month
                        FROM   payment_1
                        GROUP BY cmpny
                               , f_name
                               , m_id
                               , month
                )
        )
)
WHERE recoveries > 0
GROUP BY cmpny
       , f_name
       , month
ORDER BY 1,2,3
;

Which generates the following results:

CMPN F_NAME           MONTH            SUM(RECOVERIES)
---- ---------------- ----------- --------------------
C001 ADIOS DAOMATO    01-OCT-2009                  486
C001 ADIOS DAOMATO    01-NOV-2009                   36
C001 ADIOS DAOMATO    01-DEC-2009                 3528
C001 NADA CAPETOWN    01-OCT-2009                  144
C001 NADA CAPETOWN    01-NOV-2009                   18
C001 NADA CAPETOWN    01-DEC-2009                 6786
C001 NADA CAPETOWN    01-DEC-2010                 3150
C001 NADA NEW DIAMOND 01-DEC-2009                 3150 -- This does not match your sample output
C001 NADA NEW DIAMOND 01-DEC-2010                 3150

I think he might have an error in your example output. I think 3150 is the correct value. The sample data for 2009 and 2010 are the same.

Tags: Database

Similar Questions

  • interesting SQL and plsql problem.

    The web application has a function to generate reports based on what the user selects, which includes this as the fields to display in the report, what order to display those selected fields, etc.

    Now, one of the field called suffix field is need removed the drop-down lists SELECT and ORDER BY. Please advise on how to solve the problem when the suffix record is removed from the database, and how we can increase the index [#] of these later OrderByField?

    reportId reportField reportValue
    ====================
    1 orderByField fname [0]
    1 orderByField [1] MI
    1 lname orderBytField [2]
    1 orderByField [3] suffix

    lname orderBytField 2 [0]
    suffix of orderByField 2 [1]
    2 orderByField fname [2]
    2 orderByField [3] MI

    In the example above, delete suffix will not affect reportId 1, but for reportId 2, if we remove the suffix, how do we record like the following?
    lname orderBytField 2 [0]
    2 orderByField fname [1]
    2 orderByField [2] MI

    I am open to any suggestion, using sql and plsql. The database is 9i.

    THX.

    Hello

    You're wright, you need to replace in the order by clause:

    with d as(
    select 1 as reportId, 'orderByField[0]' as reportField, 'fname ' as reportValue from dual union all
    select 1 as reportId, 'orderByField[1]' as reportField, 'MI    ' as reportValue from dual union all
    select 1 as reportId, 'orderByField[2]'as reportField,  'lname ' as reportValue from dual union all
    select 1 as reportId, 'orderByField[3]' as reportField, 'suffix' as reportValue from dual union all
    select 2 as reportId, 'orderByField[1]' as reportField, 'suffix' as reportValue from dual union all
    select 2 as reportId, 'orderByField[2]' as reportField, 'fname2' as reportValue from dual union all
    select 2 as reportId, 'orderByField[0]'as reportField,  'lname0' as reportValue from dual union all
    select 2 as reportId, 'orderByField[6]'as reportField,  'lname6' as reportValue from dual union all
    select 2 as reportId, 'orderByField[12]'as reportField, 'lname12' as reportValue from dual union all
    select 2 as reportId, 'orderByField[4]'as reportField,  'lname4' as reportValue from dual union all
    select 2 as reportId, 'orderByField[5]'as reportField,  'lname5' as reportValue from dual union all
    select 2 as reportId, 'orderByField[8]'as reportField,  'lname8' as reportValue from dual union all
    select 2 as reportId, 'orderByField[10]'as reportField, 'lname10' as reportValue from dual union all
    select 2 as reportId, 'orderByField[11]'as reportField, 'lname11' as reportValue from dual union all
    select 2 as reportId, 'orderByField[9]'as reportField,  'lname9' as reportValue from dual union all
    select 2 as reportId, 'orderByField[7]'as reportField,  'lname7' as reportValue from dual union all
    select 2 as reportId, 'orderByField[3]' as reportField, 'MI    ' as reportValue from dual)
    SELECT a.reportId, 'reportField[' || to_char(DECODE(ROWNUM-min_sno,0,1,rownum+1-min_sno)-1)|| ']' as newReportField, reportValue FROM
    (SELECT * FROM d where d.reportValue != 'suffix' ORDER BY reportId, to_number(substr(reportField, instr(reportField, '[')+1, instr(reportField,']')-instr(reportField, '[')-1)) ) a,
    (
    SELECT reportId,MIN(rownum) min_sno FROM
    (
    SELECT * FROM d where d.reportValue != 'suffix' ORDER BY reportId, to_number(substr(reportField, instr(reportField, '[')+1, instr(reportField,']')-instr(reportField, '[')-1))
    )
    GROUP BY reportId
    ) b
    WHERE a.reportId = b.reportId;
    

    Kind regards

    Published by: Walter Fernández on January 6, 2009 16:51 - sorry I forgot the version of your database in the first post, I replaced the regular expression...

  • I want to improve my skills in sql and plsql

    Hello friends,

    I finished the course SQL and PLSQL a few weeks back. Since then I've been hunting for a relevant work for SQL / PLSQL developer.

    However I really want to improve my writing of the subprogrammes, build effective SQL queries to become a skilled oracle developer.

    My current work is not linked to this use so all my free time to improve my knowledge in this area.

    I know the basics of SQL and PLSQL concepts and can write queries simple, subprogrammes, but to become employable, according to me, that I need to learn a lot more.

    Please suggest me some good sources be it no books / sites Web, etc. anything that would really help me become a good developer.

    I'm desperate to make a career out of Oracle SQL / PLSQL!

    Thanks in advance.

    Do what most of the experts do... start at the bottom.

    Find a job in a junior role (and yes it will be low wages initially) where the company may see your potential and enthusiasm to learn and will give training and let you learn on the job.  If you have the skills to do it, then you will learn quickly and prove you are competent and deserve to be promoted to better roles.

    When I started (in the same after having a university degree in it), I always had to start at the bottom, in a junior role, on a 3 month temporary contract.  Who got so extended that they identified some new work roles that were needed, and then I had to get one of these new jobs, where I got a permanent role.  Within 18 months, they had sent me on the training, and I was promoted to a role of DBA/programmer, working alongside other programmers and hardware team to manage database servers... everything just because I had shown the ability, willingness to learn and the professional approach to my work.

    Don't think you can just get some training or the qualities of the review, but have no experience and just get into some senior roles or an expert, as most of the companies are wise to such things... they want to know what you have for the roles of the experience.

  • need to get sql and plsql certifications

    Dear Sir

    I did the track developer oracle 9i in 2008 and not not taken any which certificate online, but right now I want to get this certificate to provide to my new employer which depends on, could you pls tell me who exact route because I saw the site of oracle certification, but some are for those who already made in the past as a dimming etc but no accreditation for those who have not "taken previously.

    All Certifications | Certifications | Oracle

    Please advice.

    Thank you.

    As a general rule, I recommend that candidates continue 1Z0-144: Oracle Database 11g: program PL/SQL rather than the exam 1Z0-147. They are equivalent regarding certification obtained the passage, but 144 is newest and best essay in general.  Discuss the two in this article here:

    Oracle PL/SQL Developer Certification: 1Z0-147 or 1Z0-144?

    Studying the materials, I have compiled links to documents of study of certification secure for a number of reviews of Oracle on my site.  Links for 1Z0-047 and 1Z0-144 are:

    Oracle Certification Preparation: Details of the examination and preparation for 1Z0 - 047 resources

    Oracle Certification Preparation: Details of the examination and preparation for 1Z0 - 144 resources

  • SQL and PLSQL solution for when Bishop subordinated direct - result of date range

    Someone there SQL lying around and reduced which can merge a table of dates in a set of data which represents a mixture of dates? See below.

    DROP TABLE dummydates_tbl;

    CREATE TABLE dummydates_tbl
    (NUMBER, NUMBER OF MANAGERID EMPID, DATE STARTDATE, ENDDATE DATE);

    INSERT INTO dummydates_tbl values (9769,1234,to_date('01-JAN-2012','DD-MON-YYYY'),to_date('30-JAN-2012','DD-MON-YYYY'));
    INSERT INTO dummydates_tbl values (5512,1234,to_date('25-JAN-2012','DD-MON-YYYY'),to_date('25-FEB-2012','DD-MON-YYYY'));
    INSERT INTO dummydates_tbl values (9769,1234,to_date('10-MAR-2012','DD-MON-YYYY'),to_date('30-JUN-2012','DD-MON-YYYY'));
    INSERT INTO dummydates_tbl values (8853,1234,to_date('10-MAR-2012','DD-MON-YYYY'),to_date('30-JUN-2012','DD-MON-YYYY'));

    COMMIT;

    -Script fixed. I'm sorry.


    /*

    Need to SQL which produces a result that shows the dates that a manager had employed was

    MANAGERID STARTDATE ENDDATE
    --------- --------- -------
    1234 1 JANUARY 2012 FEBRUARY 25, 2012
    1234 10 MARCH 2012 JUNE 30, 2012

    Picture data:

    |--------------| |-------------|
    |---------|
    |-------------|

    */

    Published by: bsc7080oec on November 12, 2012 11:34

    Published by: bsc7080oec on November 12, 2012 12:37
  • Updated APEX collection error ORA-04044: procedure, function, package, type

    Hi all

    I am trying to update the APEX MEMBER of the COLLECTION and encounter errors:


    1 error has occurred
    ORA-06550: line 9, column 8: PL/SQL: ORA-04044: procedure, function, package or type is not allowed here ORA-06550: line 7, column 3: PL/SQL: statement ignored

    The PLSQL code are as below

    DECLARE
    entire x;
    v_date_started VARCHAR2 (255);
    v_date_finished VARCHAR2 (255);
    BEGIN
    SELECT c.seq_id
    X
    OF APEX_COLLECTION c
    WHERE collection_name = "PHASE_COLLECTION."
    AND c001 =: P41_MPM_ID
    and rownum = 1;
    v_date_started: =: P41_DATE_STARTED;
    v_date_finished: =: P41_DATE_FINISHED;

    APEX_COLLECTION. () UPDATE_MEMBER
    p_collection_name = > 'PHASE_COLLECTION '.
    , p_seq = > x
    , p_c004 = > v_date_started
    , P_c005 = > v_date_finished
    );
    END;


    The process will take place after SUBMIT and validation.

    The code I used to add a colletion APEX will work perfectly and is as below

    -Initialization Collection PHASE_COLLECTION
    BEGIN
    IF THIS ISN'T APEX_COLLECTION. COLLECTION_EXISTS('PHASE_COLLECTION') THEN
    APEX_COLLECTION. CREATE_COLLECTION ('PHASE_COLLECTION');
    ON THE OTHER
    APEX_COLLECTION. TRUNCATE_COLLECTION ('PHASE_COLLECTION');
    END IF;
    -Add members to the COLLECTION PHASE of table PHASE_MEMBERSHIP
    IF: P41_MPM_ENG_ID IS NOT NULL THEN
    FOR x IN (SELECT mpm_id, mpm_eng_id, phase_number, date_started, date_finished, date_created
    OF phase_membership
    WHERE mpm_eng_id =: P41_MPM_ENG_ID
    AND active = 1
    ORDER BY date_created)
    LOOP
    APEX_COLLECTION. ADD_MEMBER
    (p_collection_name = > 'PHASE_COLLECTION')
    , p_c001 = > x.MPM_ID
    , p_c002 = > x.MPM_ENG_ID
    , p_c003 = > x.PHASE_NUMBER
    , p_c004 = > x.DATE_STARTED
    , p_c005 = > x.DATE_FINISHED
    , p_c006 = > x.DATE_CREATED

    );
    END LOOP;
    END IF;
    EXCEPTION
    WHILE OTHERS THEN
    Logger.Error (p_message_text = > SQLERRM)
    , p_message_code = > SQLCODE
    , p_stack_trace = > dbms_utility.format_error_backtrace
    );
    LIFT;
    END;


    What I tried to use APEX_COLLETION to check the overlap between the date_range.
    The requirement is just to display a warning if the user adds a record to the new phase or update record of current phase may overlap between the order of phase together.
    Table PHASE_MEMBERSHIP script is

    CREATE TABLE 'PHASE_MEMBERSHIP '.
    (
    ACTIVATE THE "MPM_ID" NUMBER NOT NULL,
    ACTIVATE THE "MPM_ENG_ID" NUMBER NOT NULL,
    ACTIVATE THE "PHASE_NUMBER" NUMBER (2.0) NOT NULL,
    "DATE_STARTED" DATE NOT NULL ACTIVATE.
    DATE OF THE "DATE_FINISHED."
    'NOTES' VARCHAR2 (2000 BYTE),
    'DATE_CREATED' DATE NOT NULL ACTIVATE.
    ACTIVATE THE 'CREATED_BY' VARCHAR2 (20 BYTE) NOT NULL,
    NUMBER (2.0) 'ACTIVE' DEFAULT 1 ACTIVATE NOT NULL)

    So, basically, I want to fill the documents associated with a particular to a collection of APEX called PHASE_COLLECTION MPM_ENG_ID.
    Then when the user specify date_started and date_finished, it will update/insert the APEX collection first.
    Then the rank based on audit collection date
    http://www.Oracle-base.com/articles/Misc/overlapping-date-ranges.php
    then display the warning to ask the user to confirm message.

    But then I'm stuck at the step of updating COLLECTION of APEX.

    If anyone has any ideas on what is wrong, please help.

    Thanks in advance.

    Ann

    Your syntx error line number should give you the trick - it is not a member of the update, rather the SQL - you are referring to apex_collection, not apex_collections view dictionary.

    Scott

  • SQL and BI

    Hello Experts;

    I used some tools like Microstrategy bi, however it is still not comparable to a coding in SQL, PL/SQL, which gives a lot of flexibility, transparency, etc. So, how is it I always get customers pushing to have all of the BI process done on BI tools. Why SQL and PL/SQL, not at the forefront instead. I know that the graphical side of things put it at a disadvantage, but beside that I'm still why a little distraught.

    SQL and PLSQL can scare companies, many see a black magic (which, in some respects, can be), and it is not something that many customers feel able to learn or even tend to. On the face of things, this may seem complicated and used wrongly, can cause MAJOR problems with buinesses (poor performance, use excessive resources that affect other sessions).  Many have invested time in getting to know point and click graphical tools that can get results without feeling lost and confused by SQL, they prefer.

    However, as you rightly remember, nothing can replace SQL or PL/SQL.

    I suppose that the element of fear is a bit like why a lot of people using MS Paint to edit an image, many feel uncomfortable with it, he can do the basics, but not much, and they'll settle for that, but you could do much more with Photoshop - it's simply too scary to learn properly for most of the people so that they avoid it - they stick to what is simple and familiar.

  • learning SQL and pssql, xml, forms and report

    I learned sql and plsql I donot Xml and forms and reports learn by own by easy method

    by sql and psql we learned basic on the rise, things of time ranging from online book (another way to practice and understand the concepts)

    repo, xml, sql, forms and pssql of learning javascript:; rt learning SQL and pssql, xml, forms and reportfor beginners

  • Procedure or the view to return the list and uses inline functions?

    I have read about this for a while and do not know what is the easiest way to move forward. I need to execute a sql select statement that calculates some of the columns using the functions I wrote. Run the sql and functions within a procedure or a view would be nice, depending on what is more simple.

    With sql server you can return the result of a statement select a procedure. You can also include custom online functions.

    Say I wrote a fnEvalIsNumber function that returns 1 or 0 if the parameter passed in a certain number or not.

    Say I created a table named t_values with a single column, varchar2, containing values such as "Merced" and "Pixie" and 452.

    I want to be able to call a proc or view that returns a list of list this:

    Value IsNum
    Merced 0
    Pixie 0
    1 452

    Fairly basic but my reading here only took me something like a simple solution. I hope that the news that I had are not for some comment. Oracle 10g.

    Published by: lecaro on March 21, 2009 09:49

    There are a lot of interesting discussions on this forum dealing with procedures vs functions, here's a {message identifier: = 547397}.

  • SQL VS PLSQL privileges and subsidies

    Hi all

    Is there a difference in the privileges of how are handled in SQL a PLSQL? I have a user who can choose a SQL table, but in plsql, the same query reports "table or view does not exist. Later, the same thing happened with another user who was DBA. The user can query the view v$ session, but cannot do PLSQL procedure.
    Why is this?

    DB is 10.2.0.2

    It seems that you access through ROLES and not run like auser. If access to objects of a procedure you need direct rights. You can check if you have rights through roles as follows

    SQL > role the value none;

    SQL > "you want to test to see if it'll work in a declaration procedure.

    If you select in SQL more with none of the roles you can do it in a procedure. If you can't, you must have the privilege to a role, you must have this privilege granted directly to you.

    Thank you

  • The display of an image defined in an external PLSQL procedure that renders the code HTML and called in a region of the APEX

    I have an external PLSQL procedure that dynamically creates a report out of the HTML tags that I then called an anonymous block APEX PLSQL.  I'm making bad images with the following code:
    in the external procedure.  How do you get around that? (NOTE: the procedure is too big to store directly in the APEX)

    ....

    ' < style td = "width: auto;" "padding: 0px 5px 0px ' > '. spc_rec. SPC_VIABILITY_STATUS. "< table > ' |

    "< style td =" width: auto; " text-align: left; "padding: 0px 5px 0px" > ' | spc_rec. SPC_VIABILITY_REASON. "< table > ' |

    TD > < img src = "" #IMAGE_PREFIX #check2.gif "alt =" "/ > < table > '"

    ....

    Thanks in advance

    PaulP

    Hi Paul,.

    You can use the global variable of the APEX package below to get the image prefix in pl/sql. Of course, your procedure should be in APEX, schema analysis application.

    APEX_APPLICATION. G_IMAGE_PREFIX

    Kind regards

    Hari

  • Master-detail functionality between the regions of PL/SQL and IR possible?

    4.2.1

    THM 2

    Hi all

    We have two regions in a page. The upper region is a region of PL/SQL that lists the 5 best wages by Dept. This was not done using traditional reports because management wanted a "dashboard" as the look and feel that is already existing and what is happening in that it is a pl/sql block that has the sliders and the necessary HTML, CSS in there.

    Below that is an IR when the user clicks on the dept of region of PL/SQL, it will refresh the IR below to display all employees in this Department

    Top of the page shows region of PL/SQL

    EMP name Dept wage

    Emp1 10 10000

    EMP2 10 950

    Emp3 20 800

    The Dept field is a hyperlink.

    When we click on Dept on this region of PL/SQL

    The lower IR should be updated (without submit page)

    Output

    Emp1 10 10000

    EMP2 10 950

    It is a kind of master-detail relationship between PL/SQL and Report (Detail) (Master) area Interactive

    We can hyperlink in the SQL in pl/sql area, but you wonder how trap line and pass the dept id to the IR and get this region to refresh account?

    All suggestions from the experts?

    Thank you

    Ryan

    It is a way to pass the info in connection

    Perform the dynamic Action of the link column report

    The dynamic action can submit the session state value, and then refresh the IR.

    On either by the way, you must create dynamic PL/SQL region to solve this problem, see this

    APEX reports: layout custom with a column named (model line)

  • Business event does not trigger the PLSQL procedure... What should do?

    We need to call a plsql procedure when the workflow approval GL ended with approval. I thought I could do it by customizing the relevant business event.

    We are on EBS 12.1.3 with RDBMS: 11.2.0.3.0.

    I see the event oracle.apps.gl.Journals.journal.approve company has been disabled currently. I've enabled and created a subscription for her. Subscription was a PLSQL procedure. Currently, for objective test only thing it does is to insert a row in a table.

    Parameters of the subscription to corporate events:
    System: ORDEBS. SYSTEM.COM

    Triggering event
    Source type: Local
    Event filter: oracle.apps.gl.Journals.journal.approve


    Execution condition
    Phase: 100
    Status: enabled
    Rule data: Message

    Action type: custom
    Error on: stop blocking and cancellation

    Action
    Rule of PL/SQL function: XX_GL_APPROVE_BE_PKG. Get_Attributes
    Priority: normal

    Documentation (not sure what value to give the latter. I went with the values below).
    Owner: Name of the company
    Tag owner: custom Schema

    Using query I can see the event company is called when occurs below approval (an added line each time approval that has happened). But I don't see all the lines in the table where to insert a line. What could go wrong? How can I make sure that the procedure was called?

    Select * from WF_DEFERRED where corrid'art = 'APPS:oracle.apps.gl.Journals.journal.approve. '

    Procedure:
    CREATE OR REPLACE PACKAGE BODY APPS. XX_GL_APPROVE_BE_PKG
    AS
    PROCEDURE Get_Attributes
    IS
    BEGIN
    INSERT INTO xx.xx_test_table VALUES (' to BE');
    COMMIT;
    END Get_Attributes;
    END XX_GL_APPROVE_BE_PKG;

    Hello

    This OWNER_TAG represents an actual application under license, as "DNF" without the quotes. The business event system will send subscriptions for licensed products.

    Also, in order to detect any other possible errors please add a subscription of type error. In this way the BES will catch the error and will notify SYSADMIN of this failure, it's not silent. You can use this link to see how: https://blogs.oracle.com/oracleworkflow/entry/defining_business_events_with_synchronous

    Kind regards

    Alejandro

  • Plan does not show the local procedures/functions

    Hello

    SQLD 4.1

    I use the preview to a plsql procedure feature. The procedure provides a local procedure.

    In the window structure the local procedure does not at all. Declared variables and cursors in the local procedure are displayed, but as if they where in the main proceedings.

    Local procedures and functions should be displayed in the window structure. It would be nice if the declares the variables and cursors of a local proceeding would be arranged in suchs a means, it is visible that they belong to this local procedure, perhaps using an indentation.

    Best regards

    Joop

    Hey, Joop!

    A mysterious person, known only as "SF" for the purpose of this response, has also reported this to me. We have filed a bug on this subject, and it shows as 'fixed' in 4.1.1.

  • Difference of procedure &amp; function

    Hi all

    After citing some tutorials, I got to know that the difference between the procedure and the function is that the procedure may or may not return a value as the function must return a value to the calling program. Given below is my procedure. & He would compare the lower of the two numbers would be smaller than

    create or replace procedure findmin

    (

    x in number,

    there in numbers,

    z series

    )

    is

    Start

    if(x<y) then

    z: = x;

    on the other

    z: = y;

    end if;

    end;

    declare

    c number;

    Start

    findmin (23,45,c);

    dbms_output.put_line (c);

    end;

    Here's my function even if it does the same thing.

    create or replace function findmin_fun

    (

    x in number,

    y number

    )

    Return number

    is

    number of z;

    Start

    if(x<y) then

    z: = x;

    on the other

    z: = y;

    end if;

    Return z;

    end;

    declare

    Start

    dbms_output.put_line (findmin_fun (56,28));

    end;

    I would like to know under what are all the scenarios of the procedure and function would be used. And also I want to know could do dml as insert, update, and delete operations

    If so, could you guys explain me with some examples to expose DML operations on functions

    After citing some tutorials, I got to know the difference between the procedure & function is that the procedure may or may not return a value as the function must return a value to the calling program.

    Not quite.

    A procedure does NOT return a value - it can fill in the parameters OUT or INOUT but which is NOT considered to be "of return values.

    A function ALWAYS returns a value that can be null. The only exception is if the function throws an exception.

    My procedure is given below. It would be to compare the lower of the two numbers & would mean that smaller

    Not quite - the procedure returns NO value. You use it to try to fill one OUT parameter, but it's NOT the same.

    I would like to know under what are all the scenarios the procedure & function would be used.

    Functions can be used in SQL; procedures cannot be used in SQL.

    The functions return a value; procedures of does NOT return a value.

    Use the functions when you need to return a value; Use the procedures when you do NOT need to return a value.

    It is not possible to list 'all scenarios' for one or the other, because there may be ANY NUMBER of possible scenarios.

    And also I want to know could do dml as insert operations, update & delete

    What happened when you tried?

    The best and fastest, to learn is to actually TRY THINGS. Don't be afraid to break the Oracle by actually writing code and tests of things yourself.

    If so, could you guys explain me with some examples to expose DML operations on functions

    The Oracle documentation have a LOT of examples of using functions and procedures and include the code of the example.

    https://docs.Oracle.com/CD/E11882_01/AppDev.112/e25519/subprograms.htm#CHDBEJGF

    The internet has even more examples of code.

Maybe you are looking for