Quote:
|
Originally Posted by tmacksam
Database record
ID Price EffI Effo
1 234.00 2005-01-01 9999-12-31
2 250.00 2005-03-01 9999-12-31
3 275.00 2005-06-01 9999-12-31
|
I think your data model is inefficient in that case. The first price in the above example isn't effective from 2005-01-01 to infinity; it is only in effect until 2005-03-01. So, if you end-date the first record to have EFFO = '2005-03-01' then your query would work (well, with little modification: "...BETWEEN EFFI AND (EFFO - 1 DAY)...").
If for some reason you decide to keep you current model then your query should be different. First of all, since your upper limit is equivalent to "inifinity" whatever date you enter as a parameter it will always be less than '9999-12-31', that is the comparison with EFFO is redundant. You only need to compare the parameter with EFFI, like this:
Code:
SELECT T.PRICE
from TABLE T
WHERE T.PRODUCT_ID = ?
AND T.EFFI = (
SELECT MAX (EFFI)
FROM TABLE
WHERE T.PRODUCT_ID = PRODUCT_ID
AND EFFI <= ?
)