Sql 在数据库表中,基于一列值获取记录,该值基于其他两列
我有下表:Sql 在数据库表中,基于一列值获取记录,该值基于其他两列,sql,oracle,Sql,Oracle,我有下表: username | timestamp | distance_location1 | distance_location2 jack 1532101253000 22.2541(km) 152.652(km) 我使用的查询是 select * from table where timestamp >= (select min(distance_location1) from table AND timest
username | timestamp | distance_location1 | distance_location2
jack 1532101253000 22.2541(km) 152.652(km)
我使用的查询是
select *
from table
where timestamp >= (select min(distance_location1) from table
AND timestamp <= (select min(distance_location2) from table
我想根据时间戳列获取记录
时间戳的起始值是其中的最小距离\位置1
时间戳的结束值是最小距离的位置2
上面的查询不起作用,因为它提供了0条记录如果距离位置1是数据类型是时间戳,那么下面的查询将起作用 你把括号放错地方了
select *
from table
where timestamp >=
( select min(distance_location1 from table )
AND timestamp <= (select min(distance_location2 from table)
但如果距离位置1和距离位置2不是数据类型时间戳
select * from table
where timestamp>= (select min(timestamp) from table t where t.distance_location1=(select min(distance_location1) from table)
) and
timestamp<=( select min(timestamp) from table t where t.distance_location2=(select min(distance_location2) from table))
您需要比较时间戳,而不是距离。在Oracle中,您可以使用keep和分析功能:
select t.*
from (select t.*,
min(timestamp) keep (dense_rank first over order by distance_location1) as distance_location1_timestamp,
min(timestamp) keep (dense_rank first over order by distance_location2) as distance_location2_timestamp
from table t
) t
where timestamp >= distance_location1_timestamp and
timestamp <= distance_location2_timestamp;
在两个子查询的末尾缺少右括号。那是打字错误吗?能为你的问题添加一些样本数据吗?@TimBiegeleisen这与打字错误无关。我添加了一个示例行。就这样我有上千张唱片,@stackoverflow。您正在将时间戳与距离进行比较。这种逻辑根本没有道理。