Oracle之PL SQL有關查詢實現

2022-03-22 00:40:35 字數 2367 閱讀 1343

最近有看到網上關於排序查詢的實現有兩個自己不常用的查詢語法,關於分析函式的用法,記之,鑑之,勉之;

在乙個表裡怎麼查詢每列資料的最大值和次大值?

with

tt

as (select

case

when col1 in (1, 2) then empno else

null

endempno,

case

when col2 in (1, 2) then ename else

null

endename,

case

when col3 in (1, 2) then job else

null

endjob,

case

when col4 in (1, 2) then mgr else

null

endmgr,

case

when col5 in (1, 2) then hiredate else

null

endhiredate,

case

when col6 in (1, 2) then sal else

null

endsal,

case

when col7 in (1, 2) then comm else

null

endcomm,

case

when col8 in (1, 2) then deptno else

null

enddeptno

from (select

empno,

row_number ()

over (order

by empno desc

) col1,

ename,

row_number ()

over (order

by ename desc

) col2,

job,

row_number ()

over (order

by job desc

) col3,

mgr,

row_number ()

over (order

by mgr desc

) col4,

hiredate,

row_number ()

over (order

by hiredate desc

) col5,

sal,

row_number ()

over (order

by sal desc

) col6,

comm,

row_number ()

over (order

by comm desc

) col7,

deptno,

row_number ()

over (order

by deptno desc

) col8

from

emp))

select

max(empno) empno,

max(ename) ename,

max(job) job,

max(mgr) mgr,

max(hiredate) hiredate,

max(sal) sal,

max(comm) comm,

max(deptno) deptno

from

ttunion

allselect

min(empno) empno,

min(ename) ename,

min(job) job,

min(mgr) mgr,

min(hiredate) hiredate,

min(sal) sal,

min(comm) comm,

min(deptno) deptno

from tt

對一張表中的資料做如下操作啊,先對資料進行分分組,再在組內進行排序,再顯示每組中的前兩行

select

*from (select

d.empno,

d.deptno,

sum(sal),

rank()

over(partition by deptno order

bysum(sal) desc

) rank

from

scott.emp d

group

by d.empno, d.deptno) where rank<

3

oracle之PLSQL小習題

求n的階乘 declare n number n s number 1 i number 1 sums number 0 begin for i in 1.n loop s s i sums sums s end loop dbms output.put line n 的階乘為 s dbms out...

Oracle 之PLSQL的常見命令

1.sql structured query language sql有許多關鍵字,以下語句是常用於開頭的語句 alter insert audit lock commit noaudit comment rename create revoke delete select drop update ...

Oracle基礎之PL SQL程式塊

pl sql塊由三個部分組成 說明部分 執行部分和異常處理部分。一段完整的pl sql程式塊結構如下所示 declare 說明部分 begin 塊開始標記 執行部分 exception 異常處理部分 end 塊結束標記 說明 1 說明部分 說明部分是可選的。由關鍵字declare引出,用於定義常量 ...