请教:ORACLE查询问题

wenkey_101 2010-07-28 10:31:09
假如A,B两个表中都有一个时间字段,
怎么取到A表中时间在B表时间与B时间前2s内,并且是B表时间与B时间前2s内最近的A的这条记录。
例如A表有时间为2010-07-22 10:20:18,2010-07-22 10:20:19;2010-07-22 10:20:25,2010-07-22 10:20:26的4条记录,
B表有2010-07-22 10:20:20,2010-07-22 10:20:27的2条记录,即A表前两条与后两条数据是在B表中第一条与第二条数据的前2秒内,但我只要取到A表中时间为2010-07-22 10:20:19与2010-07-22 10:20:26这2条记录.....

请各位高手指点,谢谢!
...全文
134 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
minitoy 2010-07-28
  • 打赏
  • 举报
回复
呵呵,没看懂楼主的意思
Phoenix_99 2010-07-28
  • 打赏
  • 举报
回复
with A as(
select to_date('2010-07-22 10:20:18','yyyy-mm-dd hh24:mi:ss') t1 from dual
union all
select to_date('2010-07-22 10:20:19','yyyy-mm-dd hh24:mi:ss') t1 from dual
union all
select to_date('2010-07-22 10:20:25','yyyy-mm-dd hh24:mi:ss') t1 from dual
union all
select to_date('2010-07-22 10:20:26','yyyy-mm-dd hh24:mi:ss') t1 from dual
),B as(
select to_date('2010-07-22 10:20:20','yyyy-mm-dd hh24:mi:ss') t2 from dual
union all
select to_date('2010-07-22 10:20:27','yyyy-mm-dd hh24:mi:ss') t2 from dual
)
select t1 from A,B where abs((t2-t1)*24*60*60) < 2
wenkey_101 2010-07-28
  • 打赏
  • 举报
回复
谢谢各位,问题已经解决..
archwuke1 2010-07-28
  • 打赏
  • 举报
回复
借用1楼

with A as(
select to_date('2010-07-22 10:20:18','yyyy-mm-dd hh24:mi:ss') t1 from dual
union all
select to_date('2010-07-22 10:20:19','yyyy-mm-dd hh24:mi:ss') t1 from dual
union all
select to_date('2010-07-22 10:20:25','yyyy-mm-dd hh24:mi:ss') t1 from dual
union all
select to_date('2010-07-22 10:20:26','yyyy-mm-dd hh24:mi:ss') t1 from dual
),B as(
select to_date('2010-07-22 10:20:20','yyyy-mm-dd hh24:mi:ss') t2 from dual
union all
select to_date('2010-07-22 10:20:27','yyyy-mm-dd hh24:mi:ss') t2 from dual
)
SELECT t1 FROM
(select t1,row_number() OVER(PARTITION BY t2 ORDER BY t1 DESC) rn from A,B where abs((t2-t1)*24*60*60) <= 2) WHERE rn=1;
心中的彩虹 2010-07-28
  • 打赏
  • 举报
回复
(b.dt-a.dt)*86400=1
心中的彩虹 2010-07-28
  • 打赏
  • 举报
回复
[Quote=引用楼主 wenkey_101 的回复:]
假如A,B两个表中都有一个时间字段,
怎么取到A表中时间在B表时间与B时间前2s内,并且是B表时间与B时间前2s内最近的A的这条记录。
例如A表有时间为2010-07-22 10:20:18,2010-07-22 10:20:19;2010-07-22 10:20:25,2010-07-22 10:20:26的4条记录,
B表有2010-07-22 10:20:20,2010-07-22……
[/Quote]

a.dt=(b.dt-interval '1' second)



17,082

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧