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

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

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

熱門標簽:臨清電話機器人 話務外呼系統(tǒng)怎么樣 云南電商智能外呼系統(tǒng)價格 智能外呼系統(tǒng)復位 外東北地圖標注 大眾點評星級酒店地圖標注 拉卡拉外呼系統(tǒng) 高清地圖標注道路 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)的方法,我們內(nèi)交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 ..) 關(guān)鍵字來做:

解法三:

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

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

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

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

巨人網(wǎng)絡(luò)通訊聲明:本文標題《SQL實現(xiàn)LeetCode(185.系里前三高薪水)》,本文關(guān)鍵詞  SQL,實現(xiàn),LeetCode,185.,系里,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《SQL實現(xiàn)LeetCode(185.系里前三高薪水)》相關(guān)的同類信息!
  • 本頁收集關(guān)于SQL實現(xiàn)LeetCode(185.系里前三高薪水)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 海安县| 保德县| 沿河| 蒙自县| 青阳县| 杭州市| 曲周县| 彭州市| 富民县| 红桥区| 凤城市| 宝清县| 安乡县| 雷山县| 丹东市| 东方市| 淳安县| 资阳市| 白银市| 安化县| 宁远县| 巴青县| 遂溪县| 克拉玛依市| 苍溪县| 琼结县| 正安县| 友谊县| 麟游县| 长宁区| 青河县| 临潭县| 北碚区| 新和县| 封丘县| 上杭县| 泸州市| 嘉鱼县| 横山县| 焦作市| 高要市|