Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PL/SQL: Fast insert based on 2nd table

Posted on 2013-11-20
2
Medium Priority
?
337 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 78

Accepted Solution

by:
slightwv (䄆 Netminder) earned 1500 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

926 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