oracle的补充知识点

1.union all 与union

union和union all的区别是,union会自动压缩多个结果集合中的重复结果,而union all则将所有的结果全部显示出来,不管是不是重复。

2.

Intersect:对两个结果集进行交集操作,不包括重复行,同时进行默认规则的排序;

Minus:对两个结果集进行差操作,不包括重复行,同时进行默认规则的排序。

3.with as

其实就是把一大堆重复用到的SQL语句放在with as 里面,取一个别名,后面的查询就可以用它

with a as (select dummy from dual),
b as (select dummy from dual)
select a.dummy,b.dummy from a,b 
where a.dummy = b.dummy

4.connect by [条件2] start with [条件3]

大致写法:select * from some_table [where 条件1] connect by [条件2] start with [条件3]; 
其中 connect by 与 start with 语句摆放的先后顺序不影响查询的结果,[where 条件1]可以不需要。 
[where 条件1]、[条件2]、[条件3]各自作用的范围都不相同: 
示例: 
假如有如下结构的表:some_table(id,p_id,name),其中p_id保存父记录的id。 
select * from some_table t where t.id!=123 connect by prior t.p_id=t.id and t.p_id!=321 start with t.p_id=33 or t.p_id=66; 
对prior的说明: 
    prior存在于[条件2]中,可以不要,不要的时候只能查找到符合“start with [条件3]”的记录,不会在寻找这些记录的子节点。
要的时候有两种写法:connect by prior t.p_id=t.id 或 connect by t.p_id=prior t.id,前一种写法表示采用自上而下的搜索方式(先找父节点然后找子节点),
后一种写法表示采用自下而上的搜索方式(先找叶子节点然后找父节点)。

prior放的左右位置决定了检索是自底向上还是自顶向下.很明显以上的sql选择了自底向上,所以最终得到了根节点。

select num1,num2,level 

from carol_tmp 

start with num2=1008 

connect by  num2=prior num1 order by level desc;(1008向上)

select num1,num2,level 

from carol_tmp 

start with num2=1008 

connect by prior  num2= num1 order by level desc;(1008向下)

5.根据时间排重,取时间最大的值

select t.*
from (select a.*, row_number() over(partition by 需要排重的字段 order by 时间 desc) rw
from HOST_CURSTATUS a) t
where t.rw = 1

https://jingyan.baidu.com/article/9989c74604a644f648ecfef3.html

row_number() OVER (PARTITION BY COL1 ORDER BY COL2) 表示根据COL1分组,在分组内部根据 COL2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的)



原文地址:https://www.cnblogs.com/zyzg/p/7954088.html