Solved

PL/SQL: Fast insert based on 2nd table

Posted on 2013-11-20
2
311 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 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
Comment Utility
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 31

Expert Comment

by:awking00
Comment Utility
Can you provide some sample data and your expected output?
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now