首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在数据库表中,根据一列值获取记录,而该值则基于其他两列。

问在数据库表中,根据一列值获取记录,而该值则基于其他两列。
EN

Stack Overflow用户
提问于 2018-10-10 08:42:29
回答 2查看 38关注 0票数 0

我有下表:

代码语言:javascript
复制
 username | timestamp   | distance_location1 | distance_location2
  jack     1532101253000   22.2541(km)           152.652(km)

和我使用的查询是

代码语言:javascript
复制
 select * 
 from   table 
 where  timestamp >= (select min(distance_location1) from table 
   AND  timestamp <= (select min(distance_location2) from table

我想得到基于时间戳列的记录。

时间戳的起始值是最小distance_location1的位置。

时间戳的结束值是最小distance_location2的位置。

上面的查询无法工作,因为它提供了0条记录。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-10 08:52:16

如果distance_location1是数据类型是时间戳,那么下面的查询将在您将括号放在错误位置的地方工作。

代码语言:javascript
复制
select * 
     from   table 
     where  timestamp >=
      ( select min(distance_location1 from table )
       AND  timestamp <= (select min(distance_location2 from table)

但是如果distance_location1和distance_location2不是数据类型时间戳

代码语言:javascript
复制
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))
票数 1
EN

Stack Overflow用户

发布于 2018-10-10 11:29:05

你需要比较时间戳,而不是距离。在Oracle中,您可以使用keep和分析函数:

代码语言:javascript
复制
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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52736081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档