I want the query for this scenario will be someone help me?

I have two tables table of markets and purchase...
When a record is there in the market table, this means that pure Price shud be replaced by Mkt Price
When I take the records between two particular date
I shud get the mkt price if a record exists for a date between and to date in the table of mkt else I shud get price pure himself.
How can I do this?
Table Purchase
Code date pure price
A 2 01/01/2010
A 3 01/05/2010
A 4 10/01/2010
A 6 15/01/2010
B 15 15/01/20110


Table of markets

Code Date Mkt price
A 10 01/05/2010
A 5 01/12/2010

When I take the records between 01/01/2010 and 01/04/2010

my result should be as follows
A 2 01/01/2010


01/01/2010 and 01/10/2010

A 01/01/2010 * 10 *.
A 01/05/2010 * 10 *.
A 4 10/01/2010

01/01/2010 and 01/15/2010

A 01/01/2010 * 5 *.
A 01/05/2010 * 5 *.
01/10/2010 * 5 *.
A 6 15/01/2010
B 15 15/01/20110

Try this... NVL may not work in a scenario... also, if date of purchase is greater than the date of the contract, then he should show the purchase price and no market prices.

PRAZY@11gR1> exec :from_date:='01/01/2010';

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.00
PRAZY@11gR1> exec :to_date:='04/01/2010';

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.00

select a.code,a.pur_date,
case when pur_date <= mkt_date then
     DECODE(mkt_price,0,pur_price,mkt_price)
else
     pur_price
end  price
from purchase_table a left outer join
(
     select code,mkt_price,mkt_date from
     (
          select code,
          mkt_price,mkt_date,
          row_number() over (partition by code order by mkt_date desc) rn
          from market_table
          where mkt_date between to_date(:from_date,'DD/MM/YYYY') and to_date(:to_date,'DD/MM/YYYY')
     )
     where
     rn=1
) b
on a.code = b.code
where a.pur_date between to_date(:from_date,'DD/MM/YYYY') and to_date(:to_date,'DD/MM/YYYY')
order by a.code,a.pur_date
/

C PUR_DATE       PRICE
- --------- ----------
A 01-JAN-10          2

Elapsed: 00:00:00.00
PRAZY@11gR1> exec :to_date:='10/01/2010';

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.00
PRAZY@11gR1> /

C PUR_DATE       PRICE
- --------- ----------
A 01-JAN-10         10
A 05-JAN-10         10
A 10-JAN-10          4

Elapsed: 00:00:00.00
PRAZY@11gR1> exec :to_date:='15/01/2010';

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.01
PRAZY@11gR1> /

C PUR_DATE       PRICE
- --------- ----------
A 01-JAN-10          5
A 05-JAN-10          5
A 10-JAN-10          5
A 15-JAN-10          6
B 15-JAN-10         15

Elapsed: 00:00:00.01
PRAZY@11gR1>

Decode added printing the purchase price if the market price is 0.

HTH,
Prazy

Published by: Prazy on April 21, 2010 15:32

Tags: Database

Similar Questions

Maybe you are looking for

  • Which card to buy

    At present, in a machine I use 1500 PA ADDI - DATA digital i/o Card. I think to change the card of NEITHER. I searched the site NEITHER but could not get a similar. ID: Digital i/o board, opto-isolated, 32 e / s digital, 24 V All cards OR are 5 v / T

  • Unable to get the basic unit of the stainless steel bracelet

    Hi, I am unable; e of pop on the basic unit of the steel stainless bracelet body. I couldn't find anything online that shows the process. In addition, can I exchange the strap metallic with leather and other straps? where can I buy these? Concerning

  • Undo changes after Windows updates

    Original title: Windows 7 Home Premium On 9 September, some updates have been installed on my computer and it resulted in changes basically what Web sites look like.  How can I reverse them?

  • How to create CustomizedList

    How to create such a list... Put the Image and the name of a cell in the list

  • trying to jquery mobile (alpha2) working in a Blackberry widget

    Sorry, I'm rather new with widget development. I try to get mobile jquery working in a widget on a blackberry 9780.If anyone has tried it, please let me know if it is worth. My first question was that jquery uses dashes in filenames, which seemsbe ba