<>oracle中关于null排序的问题</P> - D. L; X7 l( s) R3 R<>问题描述:<BR>在处理一般的数据记录中,对于数字类型的字段,在oracle的排序中,默认把null值做为<BR>大于任何数字的类型,当然对于varchar2类型的字段,默认也是该处理方式,但是客户<BR>要求排序的过程中,需要把null的字段默认排在前边(从小-->大)。一般的<BR>order by xxxx,无法解决。</P> 9 `- ]9 [4 n# F- V<>问题解决:<BR>方案1:<BR>可以使用复杂的使用sql:</P>$ Z0 d, W/ D5 K3 c5 G5 S% ^
<>select * from <BR>(select a.*,rownum as my_sys_rownum from (<BR>select deptid,nvl(BDZNAME,' '),nvl(VOLLEVEL,'0'),ZBRL,nvl(ZBTS, '0'),nvl(FZR,'0'),nvl(DEPTIDDES,' '),nvl(TEL,' '),nvl(RUNSTATEDES,' '),nvl(ADDRESS,' '),BDZID from V_BDZ where rownum<2000 </P>. E( ~7 W3 ~# E4 s) L1 c
<>and ZBRL is null</P>& o) W3 s4 Z2 d w ]; b
<>) a<BR>union<BR>select b.*,rownum+(select count(*) from (<BR>select deptid,nvl(BDZNAME,' '),nvl(VOLLEVEL,'0'),ZBRL,nvl(ZBTS, '0'),nvl(FZR,'0'),nvl(DEPTIDDES,' '),nvl(TEL,' '),nvl(RUNSTATEDES,' '),nvl(ADDRESS,' '),BDZID from V_BDZ where rownum<2000 </P>$ ?, W! Z. l! j% \9 s$ Q6 g k
<>and ZBRL is null</P> m& H1 l" C/ `# u: H) E8 T<>)) as my_sys_rownum from (<BR>select deptid,nvl(BDZNAME,' '),nvl(VOLLEVEL,'0'),ZBRL,nvl(ZBTS, '0'),nvl(FZR,'0'),nvl(DEPTIDDES,' '),nvl(TEL,' '),nvl(RUNSTATEDES,' '),nvl(ADDRESS,' '),BDZID from V_BDZ where rownum<2000 </P> o: I Y) m: t1 ~4 W% W0 y1 Q( _* B i
<>and ZBRL is not null order by ZBRL <BR>) b<BR>)<BR>order by my_sys_rownum desc</P>. @4 d6 z; d+ `! e
<>方案2:<BR>可以利用oracle中可以对order by中对比较字段做设置的方式来实现:<BR> 如: ……order by nvl( aaa,'-1')</P><BR>