ORACLE Top/Bottom N、First/Last、NTile
目录
==================================================================
1.带空值的排列
2.Top/Bottom N查询
3.First/Last排名查询
4.按层次查询
一、带空值的排列:
假如被排列的数据中含有空值呢?
SQL> select region_id, customer_id,
2 sum(customer_sales) cust_sales,
3 sum(sum(customer_sales)) over(partition by region_id) ran_total,
4 rank() over(partition by region_id
5 order by sum(customer_sales) desc) rank
6 from user_order
7 group by region_id, customer_id;
REGION_ID CUSTOMER_ID CUST_SALES RAN_TOTAL RANK
---------- ----------- ---------- ---------- ----------
10 31 6238901 1
10 26 1808949 6238901 2
10 27 1322747 6238901 3
10 30 1216858 6238901 4
10 28
相关文档:
IN和EXISTS区别
in 是把外表和内表作hash join,而exists是对外表作loop,每次loop再对内表进行查询。
一直以来认为exists比in效率高的说法是不准确的。
如果查询的两个表大小相当,那么用in和exists差别不大。
如果两个表中一个较小,一个是大表,则子查询表大的用exists,子查询表小的用in:
例如:表A(小表),表B ......
由于系统移植,原来的数据库编码和时区都换了,原来的一些SQL文也出错了。。
经常崩出"ORA-01846: not a valid day of the week
"错误。
经测试,以下这个简单语句也会错!!
SQL> select next_day(sysdate,'FRIDAY') from DUAL;
select next_day(sysdate,'FRIDAY') from DUAL
ORA-01 ......
select trim(leading | trailing | both ' ' from ' abc d ') from dual;
去掉字符串 ' abc d ' 的前面/后面/前后的空格
类似函数:ltrim, ......
1. round(Num,n) : 四舍五入数字Num,保留n位小数,不写N默认不要小数,四舍五入到整数个位
select ROUND(21.237,2) from dual;
结果: 21.24
2. trunc(Num,n) : 截取数字Num,保留n位小数,不写N默认是0,即不要小数
select TRUNC(21.237,2) from dual;
结果:21.2 ......
select sysdate from dual; 从伪表查系统时间,以默认格式输出。
sysdate+(5/24/60/60) 在系统时间基础上延迟5秒
sysdate+5/24/60 在系统时间基础上延迟5分钟
sysdate+5/24 在系统时间基础上延迟5小时
sysdate+5 在系统时间基础上延迟5天
所以日期计算默认单位是天
round (sysdate,’day’) 不是四除 ......