您要求的结果不明确(您希望从哪个列表中获得最新信息?),但这里有几个想法:
在 Oracle 中执行此操作的愚蠢方法:从一个或另一个表中提取最新日期,然后进行连接:
select material, company, ..., quote_price, quote_date,
table1_latest.max_list_date, table1_latest.list_price
from detail_table
left outer join
(select max(date) max_list_date, list_price, material from table1
group by list_price, material) table1_latest
ON detail_table.material = table1_latest.material
编辑:做一个“完整”连接,然后找出日期之间的差异,使用最小的差异
select min(abs(days_diff)) min_days_diff, material, list_price,
list_date, quote_date as closest_quote_date, quote_price as closest_quote_price
from
(select list_table.material, list_table.list_price,
list_table.list_date, quote_table.quote_price,
quote_table.quote_date,
(quote_table.quote_date-list_table.list_date) days_diff
from list_table
left outer join
quote_table
on list_table.material = quote_table.material) joined_table
group by material, list_price, list_date, quote_date, quote_price
Left Outer Join 获取每个标价的所有可用报价。只要这两个日期是Oracle Date 类型,就可以做减法;它会产生天数差异。