level
語法:
select [level], column, expr...
from table
[where condition]
start with condition
connect by [prior column1= column2 | column1 = prior column2];
舉例:按照級別對emp員工進行排序:
selectlevel, emp.*
from emp
start
with ename = 'king'
connectbypriorempno = mgr
order
bylevel;
然後我們再讓它顯示的更清楚:
selectename,sys_connect_by_path(ename,'/') pename
fromemp
startwithmgr isnull
connectbypriorempno=mgr ;
使用connect_by_path(ename,'/')函式
還可以這樣,顯示更清楚:
select ename,lpad(' ', 2 * level-1)||sys_connect_by_path(ename,'/') "path"
from emp
start with mgr is null
connect by prior empno=mgr;
分析:start with 樹型結構的起點從**開始,而這裡我們就從mgr is null這也就是經理是空的人開始。然後connect by prior,這裡prior是指優先的意思。這裡就需要優先找到king的員工編號,然後再用mgr=它的員工編寫去找下乙個。
顛倒一下,我們試著讓 mgr=prior empno這樣寫看一下:
分析:首先同樣是先找到mgr is null的人,這裡很明顯還是king,因為只有king的mgr是空的。然後從這裡作為起點開始。再優先去找king的mgr,而這裡就很明顯king的mgr是空的,所以就找不到empno是空的人,然後後面也就沒法繼續找其他人了。所以這裡的結果也只有一行。
不帶prior是怎麼樣的呢?
結果一樣:
不寫prior的預設值是:
connect by prior mgr=empno;
解析:select ename,lpad(' ', 2 * level-1)||sys_connect_by_path(ename,'/') "path"
from emp
start with mgr is null
connect by prior empno=mgr;
lpad是左填充,這裡填充的是空格。根據level填充空格數。
函式:列轉行函式。很好用
wmsys.wm_concat
col empname for a60
select deptno, wmsys.wm_concat(ename) empname from emp group by deptno;
行轉列:
create table score(
name varchar2(10),
subject varchar2(10),
grade number(3)
) ;insert into score values('zhang','language',80);
insert into score values('zhang','math',92);
insert into score values('zhang','english',76);
insert into score values('li','english',50);
insert into score values('li','math',95);
insert into score values('li','language',81);
insert into score values('wang','language',73);
commit;
21:34:30
select * from score;
用11g的新特性pivot
語法:select ...
from ...
pivot [xml]
(pivot_clause
pivot_for_clause
pivot_in_clause )
where ..
select * from score
pivot (sum(grade)
for subject
in('language','english','math'));
也可以這樣寫:
select name,
sum(decode(subject,'language', grade,0)) "language",
sum(decode(subject,'math', grade,0)) "math",
sum(decode(subject,'english', grade,0)) "english"
from score
group by name;
列轉行:
方法一: 用union all
21:48:48
select name, 'language' subject,language grade from score2
union all
select name, 'math' subject,math grade from score2
union all
select name, 'english' subject,english grade from score2
order by name;
方法二: 用insert all
21:50:35
create table score3(
name varchar2(10),
subject varchar2(10),
grade number(3)
) ;
21:50:53
insert all
into score3 values(name,'language',language)
into score3 values(name,'english',english)
into score3 values(name,'math',math)
select name,language,english,math
from score2;
21:50:58
select * from score3 order by name;
方法三:用11g的新特性unpivot
語法:select ...
from ...
unpivot [include|exclude nulls]
(unpivot_clause
unpivot_for_clause
unpivot_in_clause )
where ...
21:52:37
select * from score2
unpivot
(grade for subject in(language,english,math));
CSS變形轉換 學習筆記
css的變形轉換 屬性 transform 一 translate 位移 常用 該屬性值有三種型別 translatex translatey 和 translate。translate 可以作用於已經執行了 絕對定位 position absolute 的元素,而要用 position 已經設定為...
常用資料轉換 學習筆記
使用opencv進行資料處理後返回的畫素點值為 b,g,r 使用pil.image進行資料處理後返回的畫素點的值為 r,g,b 將某點的 b,g,r 轉換為 r,g,b 用到倒敘轉換 dominant color bgr dominant color rgb 1 pil.image opencv i...
c 強制型別轉換 學習筆記
c 強制型別轉換分為四種,static cast,dynamic cast,const cast,reinterpret cast 一.為什麼在c 中還有特殊的四種強制轉換 二.static cast include iostream intmain 三.const cast include ios...