Solved

PL/SQL: Fast insert based on 2nd table

Posted on 2013-11-20
2
321 Views
Last Modified: 2013-11-25
I have three tables:
TransactionTable contains product_id, DateSold - has a couple million records
ExceptionTable contains product_id and E_Code - empty to start, will be populated based on:
ProductTable contains product_id, myCondition, LastChangeDate

ProductTable is NOT unique for product_id but is unique with product_id & LastChangeDate
TransactionTable can have multiple records for product_id on the same DateSold

I need to go through the TransactionTable, get the product_id, look into the ProductTable. If the Product_ID exists in the ProdcutTable
where the ProductTable Mycondition ='X'
I have to get the one record from the ProductTable where the maximum LastChangeDate is less than the DateSold and use this for the above comparison

The way I'm doing it now is sending each TransactionTable.product_id and the DateSold to the following and then updating the ExceptionTable where this returns 'X':

SELECT myCondition INTO tmpReturn
        FROM
            (
             SELECT myCondition
             FROM ProductTable
             WHERE ProductTable.product_id = <Passed product_id>
                 AND ProductTable.LastChangeDate = (SELECT MAX(LastChangeDate)
                                         FROM  ProductTable
                                         WHERE ProductTable.product_id = <Passed product_id>
                                             AND ProductTable.LastChangeDate <= (<Passed DateSold>))
                 AND ROWNUM = 1
            );

        CASE WHEN tmpReturn = 'X'
             THEN tmpReturn := 'Y';
             ELSE tmpReturn := 'N';
        END CASE;

       RETURN tmpReturn;

Is there a better/faster way to do this?
0
Comment
Question by:GNOVAK
2 Comments
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 39662993
Try row_number.

Something like:
select myCondition from (
SELECT myCondition, row_number() over(partition by product_id order by lastchangedate desc) myrn FROM ProductTable where...
)
where myrn=1
0
 
LVL 32

Expert Comment

by:awking00
ID: 39663077
Can you provide some sample data and your expected output?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question