Oracle - Sql - 具有其他表中最高值的条目

时间:2015-09-14 12:39:04

标签: sql oracle max

我有4张赌桌,位置,时间和检查室。

enter image description here

Car< - N:m - >时间 检查室是关系表。

我只想要日期(从时间),位置名称和具有最高运行时间的car_name。

任何人都可以提示,如何解决问题?

2 个答案:

答案 0 :(得分:0)

尝试使用此查询:

select Time.Time_date, Location.Location_name, Car.Car_name 
from Time
inner join Checkroom on Time.Time_ID = Checkroom.Time_ID
inner join Location on Checkroom.Location_ID = Location.Location_ID
inner join Car on Checkroom.Car_ID = Car.Car_ID
where Time.Time_runtime = (select MAX(Time_runtime) from Time)

答案 1 :(得分:0)

在Oracle案例中,JOIN表格ORDER BY Time_runtime DESC,使用FETCH FIRST仅获得一行 - Time_runtime最高的一行。

select Time.Time_date, Location.Location_name, Car.Car_name 
from Time
inner join Checkroom on Time.Time_ID = Checkroom.Time_ID
inner join Location on Checkroom.Location_ID = Location.Location_ID
inner join Car on Checkroom.Car_ID = Car.Car_ID
order by Time.Time_runtime DESC
FETCH FIRST 1 ROWS ONLY

FETCH FIRST仅适用于较新的Oracle版本。