将oracle 9i与SQL开发人员一起使用
如果表2中没有日期,有人可以告诉我如何使用最近日期的费率值。目前我已经硬编码6月4日(星期一)使用星期四的费率,因为没有6月4日的费率。周末很容易。但我想这样做而不必手动硬编码假日日期。有人可以帮忙吗?
select
m.effective_date,
m.value*al.rate
from table1 m, table2 al
where
M.ID = al.ID
AND M.EFFECTIVE_DATE BETWEEN '01-jun-12' and '30-jun-12'
and AL.value_date = m.EFFECTIVE_date - ( case
when m.EFFECTIVE_date = '04-jun-12' then 3
when to_char(m.EFFECTIVE_date,'DY') = 'SUN' then 2
when to_char(m.EFFECTIVE_date,'DY') = 'SAT' then 1
ELSE 0
end )
table1
Effective_date Value
06-01-2012 100
06-02-2012 200
06-03-2012 600
06-04-2012 600
All the way to 06-30-2012
Table 2 (does not have weekends and some dates depending on UK holiday)
Value_date Rate
06-01-2012 1.2
06-05-2012 1.5
06-06-2012 5.3
06-07-2012 1.5
06-08-2012 5.6
06-11-2012 1.5
06-12-2012 1.9
06-13-2012 2.1
答案 0 :(得分:0)
试试这个:
SELECT m.effective_date,
m.value*al.rate
from table1 m, table2 al
where
M.ID = al.ID
AND M.EFFECTIVE_DATE BETWEEN '01-jun-12' and '30-jun-12'
WHERE m.effective_dt = (SELECT MAX(effective_dt) FROM table1 x
WHERE x.effective_dt <= a1.value_dt)