婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > SQL實現LeetCode(185.系里前三高薪水)

SQL實現LeetCode(185.系里前三高薪水)

熱門標簽:臨清電話機器人 話務外呼系統怎么樣 云南電商智能外呼系統價格 智能外呼系統復位 外東北地圖標注 大眾點評星級酒店地圖標注 拉卡拉外呼系統 高清地圖標注道路 400電話可以辦理嗎

[LeetCode] 185.Department Top Three Salaries 系里前三高薪水

The Employee table holds all employees. Every employee has an Id, and there is also a column for the department Id.

+----+-------+--------+--------------+
| Id | Name  | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1  | Joe   | 70000  | 1            |
| 2  | Henry | 80000  | 2            |
| 3  | Sam   | 60000  | 2            |
| 4  | Max   | 90000  | 1            |
| 5  | Janet | 69000  | 1            |
| 6  | Randy | 85000  | 1            |
+----+-------+--------+--------------+

The Department table holds all departments of the company.

+----+----------+
| Id | Name     |
+----+----------+
| 1  | IT       |
| 2  | Sales    |
+----+----------+

Write a SQL query to find employees who earn the top three salaries in each of the department. For the above tables, your SQL query should return the following rows.

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Max      | 90000  |
| IT         | Randy    | 85000  |
| IT         | Joe      | 70000  |
| Sales      | Henry    | 80000  |
| Sales      | Sam      | 60000  |
+------------+----------+--------+

這道題是之前那道Department Highest Salary的拓展,難度標記為Hard,還是蠻有難度的一道題,綜合了前面很多題的知識點,首先看使用Select Count(Distinct)的方法,我們內交Employee和Department兩張表,然后我們找出比當前薪水高的最多只能有兩個,那么前三高的都能被取出來了,參見代碼如下:

解法一:

SELECT d.Name AS Department, e.Name AS Employee, e.Salary FROM Employee e
JOIN Department d on e.DepartmentId = d.Id
WHERE (SELECT COUNT(DISTINCT Salary) FROM Employee WHERE Salary > e.Salary
AND DepartmentId = d.Id)  3 ORDER BY d.Name, e.Salary DESC;

下面這種方法將上面方法中的3換成了IN (0, 1, 2),是一樣的效果:

解法二:

SELECT d.Name AS Department, e.Name AS Employee, e.Salary FROM Employee e, Department d
WHERE (SELECT COUNT(DISTINCT Salary) FROM Employee WHERE Salary > e.Salary
AND DepartmentId = d.Id) IN (0, 1, 2) AND e.DepartmentId = d.Id ORDER BY d.Name, e.Salary DESC;

或者我們也可以使用Group by Having Count(Distinct ..) 關鍵字來做:

解法三:

SELECT d.Name AS Department, e.Name AS Employee, e.Salary FROM 
(SELECT e1.Name, e1.Salary, e1.DepartmentId FROM Employee e1 JOIN Employee e2 
ON e1.DepartmentId = e2.DepartmentId AND e1.Salary = e2.Salary GROUP BY e1.Id 
HAVING COUNT(DISTINCT e2.Salary) = 3) e JOIN Department d ON e.DepartmentId = d.Id 
ORDER BY d.Name, e.Salary DESC;

下面這種方法略微復雜一些,用到了變量,跟Consecutive Numbers中的解法三使用的方法一樣,目的是為了給每個人都按照薪水的高低增加一個rank,最后返回rank值小于等于3的項即可,參見代碼如下:

解法四:

SELECT d.Name AS Department, e.Name AS Employee, e.Salary FROM 
(SELECT Name, Salary, DepartmentId,
@rank := IF(@pre_d = DepartmentId, @rank + (@pre_s > Salary), 1) AS rank,
@pre_d := DepartmentId, @pre_s := Salary 
FROM Employee, (SELECT @pre_d := -1, @pre_s := -1, @rank := 1) AS init
ORDER BY DepartmentId, Salary DESC) e JOIN Department d ON e.DepartmentId = d.Id
WHERE e.rank = 3 ORDER BY d.Name, e.Salary DESC;

類似題目:

Department Highest Salary

Second Highest Salary

Combine Two Tables

參考資料:

https://leetcode.com/discuss/23002/my-tidy-solution

https://leetcode.com/discuss/91087/yet-another-solution-using-having-count-distinct

https://leetcode.com/discuss/69880/two-solutions-1-count-join-2-three-variables-join

到此這篇關于SQL實現LeetCode(185.系里前三高薪水)的文章就介紹到這了,更多相關SQL實現系里前三高薪水內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • SQL實現LeetCode(196.刪除重復郵箱)
  • SQL實現LeetCode(184.系里最高薪水)
  • SQL實現LeetCode(183.從未下單訂購的顧客)
  • SQL實現LeetCode(182.重復的郵箱)
  • SQL實現LeetCode(181.員工掙得比經理多)
  • SQL實現LeetCode(180.連續的數字)
  • C++實現LeetCode(179.最大組合數)
  • SQL實現LeetCode(197.上升溫度)

標簽:福州 山西 定西 無錫 三明 阿里 揚州 溫州

巨人網絡通訊聲明:本文標題《SQL實現LeetCode(185.系里前三高薪水)》,本文關鍵詞  SQL,實現,LeetCode,185.,系里,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《SQL實現LeetCode(185.系里前三高薪水)》相關的同類信息!
  • 本頁收集關于SQL實現LeetCode(185.系里前三高薪水)的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 成都市| 莱芜市| 潞城市| 临湘市| 墨脱县| 江陵县| 布拖县| 灵寿县| 崇州市| 大洼县| 尉氏县| 古蔺县| 康保县| 尚义县| 阳春市| 湖北省| 土默特右旗| 柳河县| 靖安县| 岚皋县| 商城县| 潜江市| 临泽县| 齐河县| 绥宁县| 吉安市| 丰镇市| 宿迁市| 阿拉善右旗| 金沙县| 云和县| 雷山县| 吉安县| 额敏县| 固原市| 明光市| 大新县| 库车县| 江北区| 定南县| 苍梧县|