MikeAuz1979
Programmer
Hi,
Using an access pass through query to oracle (Native oracle pl sql) I'm trying to return the 2 most recent records (Max PRICE_EFFECTIVE_DATES) for each contract. (SUBCONTRACT is the field in my code.)
So the result I'm after is like:
SUBCONTRACT P_E_DATE P_P_B
1 01-jan-07 5.67
1 01-jul-07 5.87
2 23-feb-07 4.78
2 30-mar-07 4.99
Using some code I found from another website I've gotten as far as the below but I'm getting the error that the sql is not properly ended.
Also any ideas on how I can avoid repeating the sub query code?
Thanks for any help
Mike
Using an access pass through query to oracle (Native oracle pl sql) I'm trying to return the 2 most recent records (Max PRICE_EFFECTIVE_DATES) for each contract. (SUBCONTRACT is the field in my code.)
So the result I'm after is like:
SUBCONTRACT P_E_DATE P_P_B
1 01-jan-07 5.67
1 01-jul-07 5.87
2 23-feb-07 4.78
2 30-mar-07 4.99
Using some code I found from another website I've gotten as far as the below but I'm getting the error that the sql is not properly ended.
Also any ideas on how I can avoid repeating the sub query code?
Thanks for any help
Mike
Code:
select t1.SUBCONTRACT_NUMBER
, t1.PRICE_EFFECTIVE_DATE
, t1.PRICE_PER_BLOCK
from
(
SELECT sub.SUBCONTRACT_NUMBER, sd.PRICE_EFFECTIVE_DATE, bp.PRICE_PER_BLOCK
FROM CCDR.BLOCK_PRICE bp, CCDR.SUBCONTRACT sub, CCDR.SUBCONTRACT_DETAILS sd, CCDR.ACTIVE_SITE a
WHERE sd.CHARGE_TYPE='04'
AND sd.COMPONENT_TYPE='1'
AND sub.SUBCONTRACT_NUMBER = sd.SUBCONTRACT_NUMBER
AND bp.PRICE_ID = sd.PRICE_ID
AND sub.ACCOUNT_NUMBER = a.ACCOUNT_NUMBER
) as t1
inner
join
(
SELECT sub.SUBCONTRACT_NUMBER, sd.PRICE_EFFECTIVE_DATE, bp.PRICE_PER_BLOCK
FROM CCDR.BLOCK_PRICE bp, CCDR.SUBCONTRACT sub, CCDR.SUBCONTRACT_DETAILS sd, CCDR.ACTIVE_SITE a
WHERE sd.CHARGE_TYPE='04'
AND sd.COMPONENT_TYPE='1'
AND sub.SUBCONTRACT_NUMBER = sd.SUBCONTRACT_NUMBER
AND bp.PRICE_ID = sd.PRICE_ID
AND sub.ACCOUNT_NUMBER = a.ACCOUNT_NUMBER
) as t2
on t1.SUBCONTRACT_NUMBER = t2.SUBCONTRACT_NUMBER
and t1.PRICE_EFFECTIVE_DATE <= t2.PRICE_EFFECTIVE_DATE
group by
t1.SUBCONTRACT_NUMBER
, t1.PRICE_EFFECTIVE_DATE
, t1.PRICE_PER_BLOCK
having count(*) <= 2