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
)
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
)
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 ?
在最后一个括号中,
subquery语句首先将a,b用a.gh=b.gh(+)方式作成结果集。
在结果集中只要a表有数据,那么结果集中肯定有数据,与b表无关。
取出最大的sxh,如果都为空,则为某值<somesmallvalue>。
如果b表中的SXH为空,则也为某值<somesmallvalue>。两者相等,条件恒成立。看不出什么错误,所以请贴主说明导致的结果。
这样查的话,如果,b表中一个gh的两条记录的sxh都是null
那查出来的就有两条记录了
black_snail(●○) 的语句改成:
and
(
nvl(b.sxh,rowid)=(select max(nvl(b.sxh,rowid)) sxh from a,b where a.gh=b.gh(+) )
)