当前位置: 代码迷 >> Oracle开发 >> 求教,如斯的SQL语录怎样写
  详细解决方案

求教,如斯的SQL语录怎样写

热度:157   发布时间:2016-04-24 06:24:21.0
求教,如此的SQL语录怎样写
现在有表A,字段为aid,aa,ab,表B,字段为bid,baid(对应表A的aid),ba,bdate(日期)。现在想查询表a的所有内容,同时包含表b的ba,条件是
1.A.aid=b.baid(当然,这是必须的)
2.只要表b中最近的一个ba
不知道我表述清楚了没?
在线等,请大家指
------解决思路----------------------
with a(aid,
aa,
ab) as
 (select 1, 'a', 'a'
    from dual
  union all
  select 2, 'b', 'b'
    from dual
  union all
  select 3, 'c', 'c'
    from dual
  union all
  select 4, 'd', 'd'
    from dual
  union all
  select 5, 'e', 'e' from dual),
b(bid,
baid,
ba,
bdate) as
 (select 1, 1, 'a', date '2015-10-01'
    from dual
  union all
  select 2, 1, 'b', date '2015-10-03'
    from dual
  union all
  select 3, 1, 'c', date '2015-10-09'
    from dual
  union all
  select 4, 3, 'd', date '2015-10-03'
    from dual
  union all
  select 5, 3, 'e', date '2015-10-05'
    from dual
  union all
  select 6, 5, 'f', date '2015-10-01' from dual)
select aid, aa, ab, ba
  from (select a.aid,
               a.aa,
               a.ab,
               b.ba,
               row_number() over(partition by b.baid order by b.bdate desc) rn,
               b.bdate
          from a, b
         where a.aid = b.baid  --①
         )
 where rn = 1    --②
 ;

------解决思路----------------------
引用:
Quote: 引用:
select a.*,b.ba
from a left join b on a.aid=b.baid
where b.bdate=(select max(bdate) from b bb where baid=b.baid );

b.bdate=(select max(bdate)这句有问题,因为不同的baid的max(bdate)是不同的

后面有加 where baid=b.baid的,你可以测试一下.
当然3楼的相对好理解
不过这两种语句的结果有可能是不一样的,就是相同的baid的最大bdate,存在有两条记录


------解决思路----------------------
引用:
Quote: 引用:
Quote: 引用:

Quote: 引用:
with a(aid,
aa,
ab) as
 (select 1, 'a', 'a'
    from dual
  union all
  select 2, 'b', 'b'
    from dual
  union all
  select 3, 'c', 'c'
    from dual
  union all
  select 4, 'd', 'd'
    from dual
  union all
  select 5, 'e', 'e' from dual),
b(bid,
baid,
ba,
bdate) as
 (select 1, 1, 'a', date '2015-10-01'
    from dual
  union all
  select 2, 1, 'b', date '2015-10-03'
    from dual
  union all
  select 3, 1, 'c', date '2015-10-09'
    from dual
  union all
  select 4, 3, 'd', date '2015-10-03'
    from dual
  union all
  select 5, 3, 'e', date '2015-10-05'
    from dual
  union all
  select 6, 5, 'f', date '2015-10-01' from dual)
select aid, aa, ab, ba
  from (select a.aid,
               a.aa,
               a.ab,
               b.ba,
               row_number() over(partition by b.baid order by b.bdate desc) rn,
               b.bdate
          from a, b
         where a.aid = b.baid  --①
         )
 where rn = 1    --②
 ;

已经比较接近了,还存在一个问题,就是如果b表中没有对应的aid,将选不出来,我希望如果没有相应的ba为空即可,不知道如何解决!

外关联下 
with a(aid,
aa,
ab) as
 (select 1, 'a', 'a'
    from dual
  union all
  select 2, 'b', 'b'
    from dual
  union all
  select 3, 'c', 'c'
    from dual
  union all
  select 4, 'd', 'd'
    from dual
  union all
  select 5, 'e', 'e' from dual),
b(bid,
baid,
ba,
bdate) as
 (select 1, 1, 'a', date '2015-10-01'
    from dual
  union all
  select 2, 1, 'b', date '2015-10-03'
    from dual
  union all
  select 3, 1, 'c', date '2015-10-09'
    from dual
  union all
  select 4, 3, 'd', date '2015-10-03'
    from dual
  union all
  select 5, 3, 'e', date '2015-10-05'
    from dual
  union all
  select 6, 5, 'f', date '2015-10-01' from dual)
select aid, aa, ab, ba
  from (select a.aid,
               a.aa,
               a.ab,
               b.ba,
               row_number() over(partition by a.aid order by b.bdate desc) rn,  --baid 改成 aid
               b.bdate
          from a left join  b   --外关联。
         on ( a.aid = b.baid)  --①
         )
 where rn = 1    --②
 order by aid
 ;

外联也想到了,就是没把b.aid改成a.id,果然成功了!太感谢了,能否解释下
?row_number()?over(partition?by?a.aid?order?by?b.bdate?desc)?rn这句吗?


row_number() over(partition by a.aid order by b.bdate desc) rn 这个为分析函数根据a.aid分组再根据 b.bdate 进行排序 这里desc也就是说由大到小排序。
  相关解决方案