MySQL Group sort row_number () over (partition by)

Source: Internet
Author: User


drop table if exists tmp1;
CREATE TABLE TMP1 (empid int, deptid int, salary decimal (10,2));
INSERT INTO TMP1 values
(1,10,5500.00),
(2,10,4500.00),
(3,20,1900.00),
(4,20,4800.00),
(5,40,6500.00),
(6,40,14500.00),
(7,40,44500.00),
(8,50,6500.00),
(9,50,7500.00);

Select Empid,deptid,salary,rank from (

Select A.empid,a.deptid,a.salary, @rownum: [Email protected]+1,
if (@pdept =a.deptid, @rank: [Email protected]+1, @rank: =1) as rank,
@pdept: =a.deptid
From

(
Select Empid,deptid,salary from TMP1 ORDER by deptid ASC, salary ASC
A

(Select @rownum: =0, @pdept: = null, @rank: =0) b

) result;

MySQL Group sort row_number () over (partition by)

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.