select a.name,b.xl  from a,b 
                   where a.gh=b.gh(+) and 
(
 b.sxh=(select max(b.sxh) sxh from a,b  where a.gh=b.gh(+) )
 or b.sxh is null
)

解决方案 »

  1.   

    忘了说了,b表中的gh不唯一,如果记录都是NULL怎么办??
      

  2.   

    select a.name,b.xl  from a,b 
                       where a.gh=b.gh(+) and 
    (
     nvl(b.sxh,'<somesmallvalue>')=(select max(nvl(b.sxh,'<somesmallvalue>')) sxh from a,b  where a.gh=b.gh(+) )
    )Is it OK now ?
      

  3.   

    为什么不可以?请详细说明上述语句导致了什么你不想看到的结果!分析black_snail(●○)  的语句:
    在最后一个括号中,
    subquery语句首先将a,b用a.gh=b.gh(+)方式作成结果集。
    在结果集中只要a表有数据,那么结果集中肯定有数据,与b表无关。
    取出最大的sxh,如果都为空,则为某值<somesmallvalue>。
    如果b表中的SXH为空,则也为某值<somesmallvalue>。两者相等,条件恒成立。看不出什么错误,所以请贴主说明导致的结果。
      

  4.   

    不好意思,我的愿意是说要每个gh的最大sxh的值,原来没有说清楚
    这样查的话,如果,b表中一个gh的两条记录的sxh都是null
    那查出来的就有两条记录了
      

  5.   

    如果你的记录插入的顺序和sxh有关系的话,还行
    black_snail(●○) 的语句改成:
    and 
    (
     nvl(b.sxh,rowid)=(select max(nvl(b.sxh,rowid)) sxh from a,b  where a.gh=b.gh(+) )
    )