ORACLE处理排序空值
主要方法是通过使用CASE表达式来“标记”一个值是否为NULL。这里标记有两个值,一个表示NULL,一个表示非NULL。这样,只要在ORDER BY子句中增加标记列,便可以很容易的控制空值是排在前面还是排在后面,而不会被空值所干扰。
SQL> select ename,sal,comm from emp;
ENAME SAL COMM
---------- ---------- ----------
SMITH 800
ALLEN 1600 300
WARD 1250 500
JONES 2975
MARTIN 1250 1400
BLAKE 2850
CLARK 2450
SCOTT 3000
KING 5000
TURNER 1500 0
ADAMS 1100
ENAME SAL COMM
---------- ---------- ----------
JAMES 950
FORD 3000
MILLER 1300
14 rows selected.
//非空值按升序排序 空值排最后
SQL> select ename,sal,comm from
2 (
3 select ename,sal,comm,
4 case when comm is null then 0 else 1 end as is_null
5 from emp
6 ) x
7 order by is_null desc,comm
8 ;
ENAME SAL COMM
---------- ---------- ----------
TURNER 1500 0
ALLEN 1600 300
WARD 1250 500
MARTIN 1250 1400
SCOTT 3000
KING 5000
ADAMS 1100
JAMES 950
FORD 3000
MILLER 1300
BLAKE 2850
ENAME SAL COMM
---------- ---------- ----------
JONES 2975
SMITH 800
CLARK 2450
14 rows selected.
//非空值按降序排序 空值排最后
SQL> select ename,sal,comm from
2 (
3 select ename,sal,comm,
4 case when comm is null then 0 else 1 end as is_null
5 from emp
6 ) x
7 order by is_null desc,comm desc
8 ;
ENAME SAL COMM
---------- ---------- ----------
MARTIN 1250 1400
WARD 1250 500
ALLEN 1600 300
TURNER 1500 0
SCOTT 3000
KIN
相关文档:
select t.lot_number
from inv.mtl_onhand_quantities_detail t
left join mtl_system_items_b mi
on t.inventory_item_id = mi.inventory_item_id
where mi.segment1 like '101%' and mi.organization_id = 102
group by t.lot_number having count(*)=1 union
select t.lot_number||'x'
......
查询表emp中所有数据
select emp_id,rownum from emp
第一步,查询结果,rownum待定
emp_id rownum
1 ? 1
2 ? 2
3 ? 3
4&n ......
1、编写目的
使用统一的命名和编码规范,使数据库命名及编码风格标准化,以便于阅读、理解和继承。
2、适用范围
本规范适用于公司范围内所有以ORACLE作为后台数据库的应用系统和项目开发工作。
3、对象命名规范
3.1 数据库和SID
数据库名定义为系统名+模块名
★ 全局数据库名和例程SID 名要求一致
★ 因SID ......
SQL> select sal,comm from emp
2 where sal<2000;
SAL COMM
---------- ----------
800
1600 300
1250 500
1250 1400
1500 0
1100
950
1300
8 rows selected.
SQL> select sal as salary,comm as ......