查找 Table B 中相对于 Table A 日期的最新值

Finding Most Recent Value in Table B Relative to Table A's Date

我目前有两个表:table atable b

我的目17=] 列于 table a)

我假设它将是 left join,但我无法拉动 Score。我所知道的就是拉出日期:

select 
a.Entity_ID,
a.Event_Date,
max(b.date_processed) --I want to change this to the score correlated to the max date_processed
from myTable a
left join myTable b
on a.Entity_ID = b.Entity_ID and b.date_processed < a.event_date
Group By a.Entity_ID, a.Event_Date, b.Date_Processed

如有任何帮助,我们将不胜感激

我了解到您想要 tableaevent_date 之前的 tabeb 最近的分数。

一个选项使用带有行限制子句的相关子查询:

select 
    a.*,
    (
        select b.score
        from tableb b
        where b.entity_id = a.entity_id and b.date_processed <= a.event_date
        order by b.date_processed desc
        fetch first 1 row only
    ) most_recent_score
from tablea a