SQL (Row_number, rank, Dense_rank) written to the Buddies in the group

Source: Internet
Author: User


<span style= "FONT-SIZE:18PX;" > recently has limited energy, and some of the most commonly used things are recorded first. Convenient to consult and summarize later. </span>

A buddy wants to achieve the following effects:

DEPTNO ename                 SAL           TOP3      Department payroll   ----------------------------------------     KING                    5000              1            8750   2     CLARK                  2450              2            8750   2     MILLER                 1300              3            8750   2     SCOTT              1         8975     1     FORD              2         8975    1     JONES                   2975              3         8975    1     BLAKE                   2850              1          5950     3     ALLEN              2         5950      3     TURNER              3         5950     3

as can be seen, he wants to achieve the department's total salary rankings. This is going to use the analytic function that we often say. What the analytic function is, there's not much to say here. Interested friends, please check the official documents yourself.
Here are some of the features we'll implement:First , we need to find out the total salary for each department first, SQL is as follows:
Select Deptno,               ename,               sal,               sum (SAL) over (partition by Deptno order by Deptno) as Sumsal from          EMP
result set of the query:

In the second step, we will implement the final function, with sql:

Select Deptno,       ename,       Sal,       sumsal,       row_number () over (partition by Deptno ORDER by Sal) as "in-sector ranking",      Dense_rank () over (order by  Sumsal) as "department rank" from  (select Deptno,               ename,               sal,               sum (SAL) over ( Partition by Deptno order by Deptno) as sumsal from          EMP)

The result set is as follows:


The effect of the image display is the final result we want. The use effect of the correlation analysis function is attached later. It's here today, to work ....

SQL (Row_number, rank, Dense_rank) written to the Buddies in the group

Related Article

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.