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 ...
随机推荐
- I2C总线之(三)---以C语言理解IIC
为了加深对I2C总线的理解,用C语言模拟IIC总线,边看源代码边读波形: 如下图所示的写操作的时序图: 读时序的理解同理.对于时序不理解的朋友请参考“I2C总线之(二)---时序” 完整的程序如下: ...
- SCP和SFTP(都使用SSH。但SCP上传不能中断,而SFTP可以续传,这是最大区别)
不管SCP还是SFTP,都是SSH的功能之一.都是使用SSH协议来传输文件的. 不用说文件内容,就是登录时的用户信息都是经过SSH加密后才传输的,所以说SCP和SFTP实现了安全的文件传输. SCP和 ...
- android和struts2实现android文件上传
1.开发准备如下2个工具类 package org.lxh.util; import java.io.BufferedReader; import java.io.InputStreamReader; ...
- Java实现字符串反转
替换原则:index k 的值和 n-k 的值进行交换.(始终记住程序员的n.k都是字符串的实际位置.) 乘除的最基本实现还是来源于移位操作. public String reverse(String ...
- wpf 动画
1动画实现 通过控件的属性 RenderTransform 设置 (1)设置控件的变化类型,如平移变化,旋转变化等,变化起点. (2)根据属性值链接相应的动画类型,如简单动画,关键帧,路径动画以及故事 ...
- Centos 6.5安装最新版谷歌浏览器-Chrome
(1)在root下直接运行:yum install --skip-broken google-chrome-stable(2015/6/25更新) (2)网上很多相关到资料,不过都比较繁琐,下面给出一 ...
- 用Delphi 实现WebService 转
一编写服务程序 第一步:File----->New----->Other------>WebServices----->Soap Server Application 选择IS ...
- 折腾iPhone的生活——iPhone 5s 开启 assistive touch 后卡顿的问题
刚刚入手我的国行iPhone5s土狗灰,感觉倍棒~ 但是一上手就发现了一个问题:卡顿. 卡顿不仅体现在日常使用中,游戏中更加严重,当我玩水果忍者的时候,会发现切水果的画面都变得不流畅起来,这是拥有64 ...
- SRM 601(1-250pt,500pt)
DIV1 250pt 题意:有很多袋子,里面装有苹果和橘子(也可能没有),给出每个袋子里有多少个苹果,多少个橘子.如果每个袋子里含有水果的总数都不小于x个,则可以从每个袋子里都拿出x个水果(拿出苹果和 ...
- BNUOJ 34985 Elegant String 2014北京邀请赛E题 矩阵快速幂
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 题目大意:问n长度的串用0~k的数字去填,有多少个串保证任意子串中不包含0~k的 ...