LeetCode-185: sql then take the first few packet ordering

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_41885819/article/details/100559670

Topic Description: Query in the EMPLOYEE table high wages in the first three employees in each department.

Topic analysis:

The title was already described more clearly, such as a sector wages in descending order of 9000,8900,8900,8500,8000 ... then the department needs to check out the fact there are four, namely, 9000,8900,8900 , 8500. This fact is well understood, for example, the highest wages 9000, with the department of four people, so if the number of inquiries is 3, then the display which three it? So, here after the wage should go heavy then check out the top three.
If deduplication query, the number of pay slips a department of less than 3, then the information should show the department of all employees.

Problem-solving ideas:

The key point of this problem is based on the department, to re-check out the top three high-wage sector wage ranking. I began to think that under the department directly detect high wages in the first three sub-queries, and then by comparing the current three high wages and salaries.
sql probably as follows:

SELECT T.NAME DEPARTMENT,E.NAME EMPLOYEE,E.SALARY
FROM EMPLOYEE E
,DEPARTMENT T
WHERE 1=1 
AND E.DEPARTMENTID = T.ID
AND  SALARY IN (
	SELECT 当前部门前三高的工资(去重后)
)
ORDER BY T.ID,E.SALARY DESC,E.ID    

This logic is certainly right, but the overall feeling is not too efficient, especially those with IN scenes, first find out the top three high wages, and then determine whether the current wage among its columns.
Sort out ideas, in fact, may be the current wage and sector added to the current sub-queries, query the current sector wages higher than the current number (duplicates removed), this would certainly avoid a lot before efficient use IN, especially when a larger amount of data .
sql as follows:

SELECT T.NAME DEPARTMENT,E.NAME EMPLOYEE,E.SALARY
FROM EMPLOYEE E, DEPARTMENT T
WHERE 1=1 
AND E.DEPARTMENTID = T.ID
--只需特别关注以下约束
AND  (SELECT COUNT(DISTINCT E1.SALARY)
           FROM EMPLOYEE E1
            WHERE E.DEPARTMENTID = E1.DEPARTMENTID
            AND E.SALARY<= E1.SALARY
           )<=3
ORDER BY T.ID,E.SALARY DESC,E.ID      

Conditions we will pull out of view subquery

(SELECT COUNT(DISTINCT E1.SALARY)
           FROM EMPLOYEE E1
            WHERE E.DEPARTMENTID = E1.DEPARTMENTID
            AND E.SALARY<= E1.SALARY
           )<=3

In parentheses query in the current department, the number of data than the current high wages, but also includes the current salary, if the query out a number greater than 3, then the current salary certainly did not break into the top three in the current department, naturally meet the requirements.

Guess you like

Origin blog.csdn.net/qq_41885819/article/details/100559670