【发布时间】:2019-12-21 07:07:37
【问题描述】:
我有两个表需要加入和聚合。
表 1 每个日期和车辆有 1 行 表 2 每个日期和车辆可以有多行
我想加入日期和车辆,并检索大于 table1.max_job_complete_date 的 min(table2.end_times)。所需的值不一定是 table2 的最大 end_times,所以我不能简单地在分区上使用 row_number() ...
simplified visual of the two tables
我尝试了以下方法,但这并不一定会专门为该日期和驱动程序拉最大 end_time。
select
a."date",
a.vehicle,
a.max_job_complete_date,
from table1 as a
left join table2 as b
on b."date" = a."date"
and b.vehicle = a.vehicle
where b.end_times >= (select(max_load_complete_time from table1))
【问题讨论】:
-
用您正在使用的数据库标记您的问题。您只是从
left join的第一个表中选择列,因此似乎根本没有使用b。
标签: sql date join multiple-conditions