我有一张桌子A
Trade# Trade_DT
1 08/10/2013
2 08/20/2013
和表B
BaseRate EffectiveDT
1.5 08/01/2013
2.0 08/15/2013
3.0 08/25/2013
我希望有一个联接,以便在TradeDT之后获得EffectiveDT
Trade# Trade_DT BaseRate EffectiveDT
1 08/10/2013 2.0 08/15/2013
2 08/20/2013 3.0 08/25/2013
答案 0 :(得分:1)
我猜你想要Trade_Dt
后的最早生效日期。以下内容适用于任何SQL方言:
select a.*,
(select min(EffectiveDt)
from b
where b.EffectiveDt > a.TradeDt
) as EffectiveDt
from a;
编辑:
要获取b
表格中的所有值,只需将表格重新加入:
select t.Trade#, t.Trade_DT, b.BaseRate, b.EffectiveDt
from (select a.*,
(select min(EffectiveDt)
from b
where b.EffectiveDt > a.TradeDt
) as EffectiveDt
from a
) t join
b
on a.EffectiveDt = b.EffectiveDt;
答案 1 :(得分:0)
select distinct Trade,
Trade_DT,
FIRST_VALUE(BaseRate) Over (partition by Trade order by EffectiveDT desc) BaseRate,
FIRST_VALUE(EffectiveDT) Over (partition by Trade order by EffectiveDT desc) EffectiveDT
from tableA
inner join tableB
on tableA.Trade_DT >= tableB.EffectiveDT
答案 2 :(得分:0)
此代码仅使用标准SQL:
SELECT a.*, b.*
FROM TableA a
JOIN TableB b ON b.EffectiveDT =
(SELECT MIN(EffectiveDT)
FROM TableB b1
WHERE a.TradeDT < b1.EffectiveDT)