Solved

PL/SQL: Fast insert based on 2nd table

Posted on 2013-11-20
2
327 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
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
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…
Suggested Courses

632 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