【发布时间】:2016-01-01 10:31:45
【问题描述】:
如何从具有最大日期的 3 个表中获取数据
select
c.model_key, v.id, v.category_Id, v.Text1, v.Numeric1,
c.Name, fx.Currency_code, fx.Rate, fx.effective_dt
from
aps.v_m_fxrates fx
join
aps.t_m_value v on fx.Currency_code = v.Text1
join
aps.t_m_category c on v.Category_Id = c.Id
where
c.Name = 'FXRates'
and c.Model_Key = 25
and v.Text1 = fx.Currency_code
and fx.version = 2
使用上面的查询我得到了结果,但结果显示为所有effective_dt。而不是这个,我只需要选择最新的effective_dt 的记录。在下图中,有 2 条带 AED 的记录,其中最晚日期为 1999-03-31 的记录必须返回。在此之后,我不知道如何继续并进一步过滤结果以实现输出。
我也试过了
select
c.model_key, v.id, v.category_Id, v.Numeric1,
c.Name, fx.Currency_code, fx.Rate, fx.effective_dt
from
aps.v_m_fxrates fx
join
aps.t_m_value v on fx.Currency_code = v.Text1
join
aps.t_m_category c on v.Category_Id = c.Id
where
c.Name = 'FXRates'
and c.Model_Key = 25
and v.Text1 = fx.Currency_code
and fx.version = 2
and fx.effective_dt in (select MAX(effective_dt)
from aps.v_m_fxrates)
但没有返回任何内容。
实际输出:
预期输出:
【问题讨论】:
标签: sql sql-server-2008 join