Oracle Partition By 的使用

來源:互聯網
上載者:User

標籤:

1.概述

Parttion by 關鍵字是Oracle中分析性函數的一部分,它和彙總函式不同的地方在於它能夠返回一個分組中的多條記錄,兒彙總函式一般只有一條反映統計值的結果。

 

2.使用方式

 

   情境:查詢出每個部門工資最低的員工編號【每個部門可能有兩個最低的工資員工】  

create table TSALER(  userid NUMBER(10),  salary NUMBER(10),  deptid NUMBER(10))-- Add comments to the columns comment on column TSALER.userid  is ‘員工ID‘;comment on column TSALER.salary  is ‘工資‘;comment on column TSALER.deptid  is ‘部門ID‘;insert into TSALER (工號, 工資, 部門編號)values (1, 200, 1);insert into TSALER (工號, 工資, 部門編號)values (2, 2000, 1);insert into TSALER (工號, 工資, 部門編號)values (3, 200, 1);insert into TSALER (工號, 工資, 部門編號)values (4, 1000, 2);insert into TSALER (工號, 工資, 部門編號)values (5, 1000, 2);insert into TSALER (工號, 工資, 部門編號)values (6, 3000, 2);
View Code

  

查詢結果:

2.1方法一
select tsaler.* from tsaler inner join(select min(salary) as salary,deptid from tsaler group by deptid) con tsaler.salary=c.salary and tsaler.deptid=c.deptid 
2.2方法二
select * from tsaler inner join(select min(salary) as salary,deptid from tsaler group by deptid) cusing(salary,deptid)
2.3方法三
--row_number() 順序排序select row_number() over(partition by deptid order by salary) my_rank ,deptid,USERID,salary from tsaler;--rank() (跳躍排序,如果有兩個第一層級時,接下來是第三層級)select rank() over(partition by deptid order by salary) my_rank,deptid,USERID,salary from tsaler;--dense_rank()(連續排序,如果有兩個第一層級時,接下來是第二級)select dense_rank() over(partition by deptid order by salary) my_rank,deptid,USERID,salary from tsaler;-------方案3解決方案select * from (select rank() over(partition by deptid order by salary) my_rank,deptid,USERID,salary from tsaler) where my_rank=1;select * from (select dense_rank() over(partition by deptid order by salary) my_rank,deptid,USERID,salary from tsaler) where my_rank=1;

 

 

Oracle Partition By 的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.