Use of Oracle Partition by

Source: Internet
Author: User

1.Overview

The parttion by keyword is part of the analytic function in Oracle , which differs from the aggregation function in that it can return multiple records in a group, and the child aggregation function generally has only one result that reflects the statistical value.

2. How to use

Scenario: Query the employee number with the lowest wage per department "there may be two minimum wage employees per department"

Create TableTsaler (userid Number(Ten), Salary Number(Ten), DeptID Number(Ten))--Add Comments to the columnsComment on columnTsaler.userid is 'Employee ID'; Comment on columntsaler.salary is 'Wages'; Comment on columnTsaler.deptid is 'Department ID';Insert  intoTsaler (work number, salary, department number)Values(1, $,1);Insert  intoTsaler (work number, salary, department number)Values(2, -,1);Insert  intoTsaler (work number, salary, department number)Values(3, $,1);Insert  intoTsaler (work number, salary, department number)Values(4, +,2);Insert  intoTsaler (work number, salary, department number)Values(5, +,2);Insert  intoTsaler (work number, salary, department number)Values(6, the,2);
View Code

Query Result:

2.1 Method One
Select Tsaler. *  from  Innerjoin(selectminas fromgroup  by DeptID) C on tsaler.salary= and Tsaler.deptid=
2.2 Method Two
Select *  from  Innerjoin(selectminas fromgroup  by DeptID) cusing (Salary,deptid)
2.3 Method Three
--row_number () sequential sortingSelectRow_number () Over(Partition byDeptIDOrder  bySalary) My_rank, deptid,userid,salary fromTsaler;--rank () (jump sort, if there are two first levels, then the third level)SelectRank () Over(Partition byDeptIDOrder  bySalary) My_rank,deptid,userid,salary fromTsaler;--Dense_rank () (sequential order, if there are two first levels, then the second level)SelectDense_rank () Over(Partition byDeptIDOrder  bySalary) My_rank,deptid,userid,salary fromTsaler;-------Solution 3Select *  from(SelectRank () Over(Partition byDeptIDOrder  bySalary) My_rank,deptid,userid,salary fromTsaler)whereMy_rank=1;Select *  from(SelectDense_rank () Over(Partition byDeptIDOrder  bySalary) My_rank,deptid,userid,salary fromTsaler)whereMy_rank=1;

Use of Oracle Partition by

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.