SQL - 合并到下一个可用日期

时间:2014-11-11 14:21:27

标签: sql join coalesce

希望足够简单却无法找到答案。试图运行:

SQL Server

left outer join exchanges fx (nolock) on 
v1.ccy = fx.ccy and v1.date = fx.date

问题是fx.ccy可能不存在于fx.date上。因此,我想加入下一个可用日期。

非常感谢,

3 个答案:

答案 0 :(得分:0)

可能的解决方案:

....
left outer join exchanges fx (nolock) on 
v1.ccy = fx.ccy and fx.date = (
    select min(date) from exchanges where date >= v1.date and ccy = v1.ccy)

答案 1 :(得分:0)

left outer join exchanges fx (nolock) on 
v1.ccy = fx.ccy and fx.date = (select min(date) 
                                 from exchanges x 
                                where v1.ccy = x.ccy 
                                  and x.date >= v1.date)

答案 2 :(得分:0)

找到解决方案。

left outer join exchanges fx (nolock) on
v1.ccy = fx.ccy and 
(select min(date) from exchanges (nolock) where date >= v1.date and ccy = v1.ccy) = fx.date

感谢您的帮助。