I have a problem getting the right “Price” for a product based on Effectivity date.
Example, I have 2 tables:
a. “Transaction” table –> this contains the products ordered, and
b. “Item Master” table –> this contains the product prices and effectivity dates of those prices
Inside the Trasaction table:
INVOICE_NO INVOICE_DATE PRODUCT_PKG_CODE PRODUCT_PKG_ITEM 1234 6/29/2009 ProductA ProductA-01 1234 6/29/2009 ProductA ProductA-02 1234 6/29/2009 ProductA ProductA-03Inside the "Item_Master" table: PRODUCT_PKG_CODE PRODUCT_PKG_ITEM PRODUCT_ITEM_PRICE EFFECTIVITY_DATE ProductA ProductA-01 25 6/1/2009 ProductA ProductA-02 22 6/1/2009 ProductA ProductA-03 20 6/1/2009 ProductA ProductA-01 15 5/1/2009 ProductA ProductA-02 12 5/1/2009 ProductA ProductA-03 10 5/1/2009 ProductA ProductA-01 19 4/1/2009 ProductA ProductA-02 17 4/1/2009 ProductA ProductA-03 15 4/1/2009
In my report, I need to display the Invoices and Orders, as well as the Price of the Order Item which was effective at the time it was paid (Invoice Date). My query looks like this (my source db is Oracle):
SELECT T.INVOICE_NO, T.INVOICE_DATE, T.PRODUCT_PKG_CODE, T.PRODUCT_PKG_ITEM, P.PRODUCT_ITEM_PRICE FROM TRANSACTION T, ITEM_MASTER P WHERE T.PRODUCT_PKG_CODE = P.PRODUCT_PKG_CODE AND T.PRODUCT_PKG_ITEM = P.PRODUCT_PKG_ITEM AND P.EFFECTIVITY_DATE <= T.INVOICE_DATE AND T.INVOICE_NO = '1234';
…which shows 2 prices for each item.
I did some other different query styles
but to no avail, so I decided
it’s time to get help. 🙂
Thanks to any of you who can
share your knowledge. –CJ–
p.s. Sorry, my post doesn’t even look right! 😀
Analytics is your friend. You can use the
FIRST_VALUE()function, for example, to get all theproduct_item_prices for the given product, sort byeffectivity_date(descending), and just pick the first one. You’ll need a DISTINCT as well so that only one row is returned for each transaction.