oracle大神们过来来看看了

清茶&浊酒 2016-07-14 04:35:52
前提:表当初设计的有点坑,但根据实际需要在现有表结构上写出想要的语法
问题:A左关联B,B中的数据关联后排序取第一个----先用on的条件过滤,在排序取第一
A表
start_link end_link date id
5 -1 2016/7/13 5
4 5 2016/7/12 4
3 4 2016/7/11 3
5 6 2016/7/10 2
4 5 2016/7/09 1


想要的结果

传入id 5获取出来4(重点不要1数据)


各位大神帮帮忙喽~~~
...全文
188 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
js14982 2016-07-14
  • 打赏
  • 举报
回复
id=2,state=1,a表限定 state=2,哪来的记录
js14982 2016-07-14
  • 打赏
  • 举报
回复
引用 11 楼 lxp520llq 的回复:
[quote=引用 10 楼 js14982 的回复:]

select b.* from  
(
      select * from A  where state=2
)A
left join 
(
      select *,row_number()over(partition by end_link order by date desc) rn from A  where state=1
)B
on A.start_link=B.end_link     
where A.id=5
and b.rn=1

加一个rn列,筛选rn=1的
这个你传入id 5跟2 获取的是一样的啊 想要的结果是 传入5返回4数据 传入2 返回1[/quote] with tt as (select 5 as start_link, -1 as end_link,'2016/7/13' as date1,5 as id ,2 as state from dual union all select 4 as start_link, 5 as end_link,'2016/7/12' as date1,4 as id ,1 as state from dual union all select 3 as start_link, 4 as end_link,'2016/7/11' as date1,3 as id ,2 as state from dual union all select 5 as start_link, 6 as end_link,'2016/7/10' as date1,2 as id ,1 as state from dual union all select 4 as start_link, 5 as end_link,'2016/7/09' as date1,1 as id ,2 as state from dual) select b.* from ( select * from tt where state=2 ) A left join ( select tt.*,row_number()over(partition by end_link order by date1 desc) rn from tt where state=1 )B on A.start_link=B.end_link where b.rn=1 a.id=2; 这样? 可是你的A表(子查询表)根本没有id=2的啊?
清茶&浊酒 2016-07-14
  • 打赏
  • 举报
回复
引用 10 楼 js14982 的回复:

select b.* from  
(
      select * from A  where state=2
)A
left join 
(
      select *,row_number()over(partition by end_link order by date desc) rn from A  where state=1
)B
on A.start_link=B.end_link     
where A.id=5
and b.rn=1

加一个rn列,筛选rn=1的
这个你传入id 5跟2 获取的是一样的啊 想要的结果是 传入5返回4数据 传入2 返回1
js14982 2016-07-14
  • 打赏
  • 举报
回复

select b.* from  
(
      select * from A  where state=2
)A
left join 
(
      select *,row_number()over(partition by end_link order by date desc) rn from A  where state=1
)B
on A.start_link=B.end_link     
where A.id=5
and b.rn=1

加一个rn列,筛选rn=1的
ghx287524027 2016-07-14
  • 打赏
  • 举报
回复
把你表结构和测试数据贴出来呀,哪里有state字段。。。。。
ghx287524027 2016-07-14
  • 打赏
  • 举报
回复
引用 6 楼 lxp520llq 的回复:
[quote=引用 3 楼 ghx287524027 的回复:] 源数据和描述都很乱呀,梳理清楚一些呗~
两张表关联查询or一张表自关联查询,左侧为主数据,右侧为从数据,从数据有多条,但是我只要距离主数据最近的一条从数据[/quote] 不觉明历
嘚嗒搬运工 2016-07-14
  • 打赏
  • 举报
回复
引用 5 楼 lxp520llq 的回复:
[quote=引用 2 楼 A_A_apan 的回复:] 看蒙圈了!!!
两张表关联查询or一张表自关联查询,左侧为主数据,右侧为从数据,从数据有多条,但是我只要距离主数据最近的一条从数据[/quote] 对你所谓的从表数据做个预处理 看看这个:
Row_Number() Over(Partition By  Order By )
清茶&浊酒 2016-07-14
  • 打赏
  • 举报
回复
引用 3 楼 ghx287524027 的回复:
源数据和描述都很乱呀,梳理清楚一些呗~
两张表关联查询or一张表自关联查询,左侧为主数据,右侧为从数据,从数据有多条,但是我只要距离主数据最近的一条从数据
清茶&浊酒 2016-07-14
  • 打赏
  • 举报
回复
引用 2 楼 A_A_apan 的回复:
看蒙圈了!!!
两张表关联查询or一张表自关联查询,左侧为主数据,右侧为从数据,从数据有多条,但是我只要距离主数据最近的一条从数据
清茶&浊酒 2016-07-14
  • 打赏
  • 举报
回复
引用 1 楼 js14982 的回复:
啥意思?不是很明白? 要去什么样的数据,b表数据是什么样的
上面的描述有点问题 不好意思 ,没有B表 ,A表ID 1、3、5状态为2 2、4状态为1 select b.* from ( select * from A where state=2 )A left join ( select * from A where state=1 )B on A.start_link=B.end_link where A.id=5------------------------结果为ID为4的数据 简单点讲就是,如果在左关联时,关联的右表数据如何只取按时间排序后的第一条
ghx287524027 2016-07-14
  • 打赏
  • 举报
回复
源数据和描述都很乱呀,梳理清楚一些呗~
嘚嗒搬运工 2016-07-14
  • 打赏
  • 举报
回复
看蒙圈了!!!
js14982 2016-07-14
  • 打赏
  • 举报
回复
啥意思?不是很明白? 要去什么样的数据,b表数据是什么样的

3,491

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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