Home » SQL & PL/SQL » SQL & PL/SQL » Please help me to rewrite this (Oracle 11g)
Please help me to rewrite this [message #604660] Thu, 02 January 2014 07:47 Go to next message
ramya_162
Messages: 107
Registered: August 2013
Location: Banglore
Senior Member
Hi Experts,

My requirement is I have to calculate sum for 3 fields from DRUG_MASTER table
and update the DRUG_REPORT table with those values.

For that I have created one temporary table DRUG_TMP I am calculating SUM and first inserting into that temp table
and using that temporary table DRUG_TMP I am updating DRUG_REPORT table.

Is there any simple way to do this without using TEMP table.
Since inserting and then updating using that table it will be a performance issue.


CREATE OR REPLACE PROCEDURE DRUG_SALE_SUM
(
    p_d_id            IN  NUMBER
)
DELETE FROM DRUG_TMP WHERE D_ID = p_d_id;
 
 
INSERT INTO DRUG_TMP (D_ID, TEAM_ID, SOLD, LOC, REGION, REV, MARGIN, UNIT)
    SELECT D_ID, IN_PATIENT_ID, 1 SOLD, LOC, REGION,
        SUM(REV * RATE) AS SUM_REVENUE,
        SUM(MARGIN * RATE) AS SUM_MARGIN,
        SUM(UNIT) AS SUM_UNIT
    FROM DRUG_MASTER
    WHERE (DRUG_CANCL IS NULL OR DRUG_CANCL <>'N')
    AND D_ID = p_d_id
    GROUP BY D_ID, IN_PATIENT_ID, LOC, REGION;
 
    INSERT INTO DRUG_TMP (D_ID, TEAM_ID, SOLD, LOC, REGION, REV, MARGIN, UNIT)
    SELECT D_ID, OUTSIDE_TEAM_ID, 2 SOLD, LOC, REGION,
        SUM(REV * RATE) AS SUM_REVENUE,
        SUM(MARGIN * RATE) AS SUM_MARGIN,
        SUM(UNIT) AS SUM_UNIT
    FROM DRUG_MASTER
    WHERE (DRUG_CANCL IS NULL OR DRUG_CANCL <>'N')
    AND D_ID = p_d_id
    GROUP BY D_ID, OUTSIDE_TEAM_ID, LOC, REGION;
    COMMIT;
 
    UPDATE drug_report DP
    SET
             (DP.TODAY_REV,
             DP.TODAY_MARGIN,
             DP.TODAY_UNIT
             ) =
                (SELECT   t.rev, t.margin, t.unit
                   FROM   DRUG_TMP t
                  WHERE       DP.D_ID = t.D_ID
                          AND DP.team_id = t.team_id
                          AND DP.SOLD = t.SOLD
                          AND DP.SEGMENT = t.REGION
                          AND DP.LOC = t.LOC
                          AND t.D_ID = p_d_id)
    WHERE   DP.snapshot_id = p_new_snapshotid
            AND DP.D_ID = p_d_id
            AND (DP.D_ID, DP.team_id, DP.SOLD, DP.SEGMENT, DP.LOC) IN
                      (SELECT   D_ID,
                                team_id,
                                SOLD,
                                REGION,
                                LOC
                         FROM   DRUG_TMP);

Please help me.

Thanks.
Re: Please help me to rewrite this [message #604661 is a reply to message #604660] Thu, 02 January 2014 08:03 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Since we do not have your tables or data, we can not run, test, debug or improve posted code.
Re: Please help me to rewrite this [message #604662 is a reply to message #604661] Thu, 02 January 2014 08:04 Go to previous messageGo to next message
ramya_162
Messages: 107
Registered: August 2013
Location: Banglore
Senior Member
Hi,

At Least please provide me the code as per my requirement.

I will test it.

Thanks.
Re: Please help me to rewrite this [message #604663 is a reply to message #604662] Thu, 02 January 2014 08:24 Go to previous message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
more often than not it is a BAD Design to store any computed value inside static table since there is no guarantee that value stays current & correct.
Previous Topic: don't want to print line if records are not found
Next Topic: Reading a csv file
Goto Forum:
  


Current Time: Thu Apr 25 20:26:43 CDT 2024