TSQL Beginners Challenge 1 - Find the second highest salary for each department
很久以前准备写的系列文章,后来因为懒一直耽搁着,今天突然决定继续下去,于是有了这篇文章,很基础,但很常用。题目描述依然拷贝。简单来说就是找出个个部门薪水排名第二的人,排名相同的要一起列出来。
Introduction
The challenge is to find the employees with the second highest salary in each department. However, it is a little more complicated because if two employees have the same salary, you need to list both of them.
Sample Data
01.EmployeeID EmployeeName Department Salary 02.----------- --------------- --------------- ---------03.1 T Cook Finance 40000.0004.2 D Michael Finance 25000.0005.3 A Smith Finance 25000.0006.4 D Adams Finance 15000.0007.5 M Williams IT 80000.0008.6 D Jones IT 40000.0009.7 J Miller IT 50000.0010.8 L Lewis IT 50000.0011.9 A Anderson Back-Office 25000.0012.10 S Martin Back-Office 15000.0013.11 J Garcia Back-Office 15000.0014.12 T Clerk Back-Office 10000.00Expected Results
1.EmployeeID EmployeeName Department Salary 2.----------- --------------- --------------- ---------3.10 S Martin Back-Office 15000.004.11 J Garcia Back-Office 15000.005.2 D Michael Finance 25000.006.3 A Smith Finance 25000.007.7 J Miller IT 50000.008.8 L Lewis IT 50000.00 Rules
- The solution should work on SQL Server 2005 and above.
- The output should be ordered by Salary.
The Answe:
if OBJECT_ID('Employees') is not null
drop table Employees;
create table Employees (
EmployeeID INT IDENTITY,
EmployeeName VARCHAR(15),
Department VARCHAR(15),
Salary NUMERIC(16,2)
)
INSERT INTO Employees(EmployeeName, Department, Salary)
VALUES('T Cook','Finance', 40000)
INSERT INTO Employees(EmployeeName, Department, Salary)
VALUES('D Michael','Finance', 25000)
INSERT INTO Employees(EmployeeName, Department, Salary)
VALUES('A Smith','Finance', 25000)
INSERT INTO Employees(EmployeeName, Department, Salary)
VALUES('D Adams','Finance', 15000)
INSERT INTO Employees(EmployeeName, Department, Salary)
VALUES('M Williams','IT', 80000)
INSERT INTO Employees(EmployeeName, Department, Salary)
VALUES('D Jones','IT', 40000)
INSERT INTO Employees(EmployeeName, Department, Salary)
VALUES('J Miller','IT', 50000)
INSERT INTO Employees(EmployeeName, Department, Salary)
VALUES('L Lewis','IT', 50000)
INSERT INTO Employees(EmployeeName, Department, Salary)
VALUES('A Anderson','Back-Office', 25000)
INSERT INTO Employees(EmployeeName, Department, Salary)
VALUES('S Martin','Back-Office', 15000)
INSERT INTO Employees(EmployeeName, Department, Salary)
VALUES('J Garcia','Back-Office', 15000)
INSERT INTO Employees(EmployeeName, Department, Salary)
VALUES('T Clerk','Back-Office', 10000)
;with cte as
(
select num= RANK() over(partition by department order by salary desc), * from Employees
)
select EmployeeID,EmployeeName,Department,salary from cte where num=2
用Rank()函数很轻松就可以达到要求,后来看了下别的方式,觉得用Row_number()也可以,写的代码虽然比较长,但是在思路上却是极好的:
;with cte as
(
select num= ROW_NUMBER()over(partition by Department order by salary desc ) ,* from Employees
),
cte2 as
(
select * from cte where num=2
)
select a.EmployeeID,a.EmployeeName,a.Department,a.salary from cte a,cte2 b
where a.Salary=b.Salary and a.Department=b.Department
order by Salary
还看到一种方式,说实话没有看懂是怎么个回事,麻烦看到的童鞋给解释下:
SELECT *
FROM Employees
WHERE CAST(salary AS VARCHAR(10)) + department IN (
SELECT CAST(MAX(salary) AS VARCHAR(10)) + department
FROM Employees o
WHERE salary < ( SELECT MAX(salary)
FROM Employees a
WHERE a.department = o.department
)
GROUP BY department )
TSQL Beginners Challenge 1 - Find the second highest salary for each department的更多相关文章
- TSQL Beginners Challenge 3 - Find the Factorial
这是一个关于CTE的应用,这里我们用CTE实现阶乘 Factorial,首先来看一个简单的小实验,然后再来看题目.有的童鞋会问怎么没有2就来3了呢,惭愧,TSQL Beginners Challeng ...
- leetcode Database3(Nth Highest Salary<—>Consecutive Numbers<—>Department Highest Salary)
一.Nth Highest Salary Write a SQL query to get the nth highest salary from the Employee table. +----+ ...
- [LeetCode] Department Highest Salary 系里最高薪水
The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...
- [LeetCode] Nth Highest Salary 第N高薪水
Write a SQL query to get the nth highest salary from the Employee table. +----+--------+ | Id | Sala ...
- [LeetCode] Second Highest Salary 第二高薪水
Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | S ...
- LeetCode 176 Second Highest Salary mysql,select 嵌套 难度:1
https://leetcode.com/problems/second-highest-salary/ Write a SQL query to get the second highest sal ...
- Leetcode 176. Second Highest Salary
Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | S ...
- find the Nth highest salary(寻找第N高薪水)
Suppose that you are given the following simple database table called Employee that has 2 columns na ...
- [SQL]LeetCode176. 第二高的薪水 | Second Highest Salary
Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | S ...
随机推荐
- vim文本编辑器的总结
说起文本编辑器,在下就不得不说vim这个老古董了,说老吧,但也是老当益壮,至今还是有不少拥护者.的确,vim很实用,在下就先列出vi的优点吧. 1.如果精通了vim,就可以省去鼠标了. 2.vim是跨 ...
- 网络流CodeForces. Original 589F:Gourmet and Banquet
A gourmet came into the banquet hall, where the cooks suggested n dishes for guests. The gourmet kno ...
- HDU-1236 排名
http://acm.hdu.edu.cn/showproblem.php?pid=1236 学会怎样按字典序排序的模板. 排名 Time Limit: 2000/1000 MS (Java/Othe ...
- Linux学习笔记19——信号2
上一节中讲到了sigprocmask函数,它的作用是检查或修改它的进程信号掩码,这一节我们主要学习捕捉与忽略信号的函数sigaction和等待信号函数. 一 sigaction函数的作用 定义在接收 ...
- word中MathType公式不能 二次编辑解决方案
问题:当新建文档然后插入公式,此时可以利用mathtype进行编辑,保存后推出第二次打开,双击公式却发现不能编辑公式. 解决方案: ////////////////////////////////// ...
- DBI接口和DPI接口的区别
1)DBI接口 A,也就是通常所讲的MCU借口,俗称80 system接口.The lcd interface between host processor and LCM device list a ...
- 在octopress中gist tab不能正确的插入gist代码
今天尝试用Octopress的gits tab插件来把gist插入到博客中,但是发现没有插入成功,调用rake generate报如下的错误: Gist replied with 404 for ht ...
- 来看看Github上流行的编码规范
Popular Coding Convention on Github是一个有趣的网站,它根据Github的代码提交情况分析了几种语言流行的代码规范,目前支持对JavaScript,Java,Py ...
- Linux下对后台进程通过kill传递信号不起作用的问题
在阅读APUE信号一章时,我遇到Linux下对后台进程通过kill传递信号不起作用的问题 具体情形与如下链接中的老兄一致: http://bbs.csdn.net/topics/390335913?p ...
- OC的单例模式的实现
下面是在ARC,GCD下的单例模式实现: 头文件里申明类方法getInstance: #import <Foundation/Foundation.h> @interface Single ...