問題描述:
在處理一般的數據記錄中,對於數字類型的字段,在Oracle的排序中,默認把null值做為大於任何數字的類型,當然對於varchar2類型的字段,默認也是該處理方式,但是客戶要求排序的過程中,需要把null的字段默認排在前邊(從小-->大)。一般的order by xxxx,無法解決。
問題解決:
方案1:
可以使用復雜的使用sql:
select * from
(select a.*,rownum as my_sys_rownum from (
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
and ZBRL is null
) a
union
select b.*,rownum+(select count(*) from (
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
and ZBRL is null
)) as my_sys_rownum from (
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
and ZBRL is not null order by ZBRL
) b
)
order by my_sys_rownum desc
方案2:
可以利用Oracle中可以對order by中對比較字段做設置的方式來實現:
如: ……order by nvl( aaa,'-1')