当前位置导航:炫浪网>>网络学院>>编程开发>>C++教程>>C++基础入门教程

Order by的数值型灵活使用

  代码:
select * from table_a where id=p_id order by decode(函数,'asc',1,'desc',-1)*jsny;  

控制试图的访问时间:
6. 代码:
create view ...
as
select ... from where exists(select x from dual where sysdate>=8:00am and sysdate<=5:00pm)

妙用decode实现排序
代码:
select * from tabname
order by decode(mode,'FIFO',1,-1)*to_char(rq,'yyyymmddhh24miss');
select * from tabname
order by decode(mode,'FIFO',rq-sysdate, sysdate-rq)  
找出某个时期内工作日数:
代码:
select count(*)
from ( select rownum-1 rnum
from all_objects
where rownum <= to_date('2002-02-28','yyyy-mm-dd') - to_date('2002-
02-01','yyyy-mm-dd')+1 )
where to_char( to_date('2002-02-01','yyyy-mm-dd')+rnum-1, 'D' ) not
in ( '1', '7' )

我觉得查询重复记录的语句就很经典
代码:
select rowid,bdsszd from BADWDJ a where a.rowid != (select max(rowid) from BADWDJ b where a.bdsszd =b.bdsszd)  
由它引申的就有很多有用的语句,如昨天回答别人的排序的难题
代码:
select id,bdsszd from BADWDJ a where a.id = (select max(id) from BADWDJ b where a.bdsszd =b.bdsszd) order by id

树型结构表的查询:
代码:
select ID,PARENT_ID from parent_child
connect by prior id = parent_id
start with id = 1;

1.decode这个函数一定需要会,我觉得sql的灵活很多地方都是通过这个function来体现的,相当于if,很好用。
2.group by,这个东东想想简单,其实好多统计功能是离不开这个操作的。oracle8中扩充了group by rollup和cube的操作。有时候省了你好多功夫的。值得注意的是,当你对事物做过有效的人为归并之后执行group by 往往会更让人心旷神怡。
3.很表竖置的经典写法,也要记住: 代码:
sum(decode( )) group by ...
注意:需要在一个subquery中确定一个横置判点。
4.树形结构表的遍历写法: 代码:
select ...from ....
start with ... connect by prior  
(父子关系表达式)
代码:
select * from xxx where decode(:var,null,'0',column1) = decode(:var,null,'0',:var);

共4页 首页 上一页 1 2 3 4 下一页 尾页 跳转到
相关内容
赞助商链接