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.00
04.
2 D Michael Finance 25000.00
05.
3 A Smith Finance 25000.00
06.
4 D Adams Finance 15000.00
07.
5 M Williams IT 80000.00
08.
6 D Jones IT 40000.00
09.
7 J Miller IT 50000.00
10.
8 L Lewis IT 50000.00
11.
9 A Anderson Back-Office 25000.00
12.
10 S Martin Back-Office 15000.00
13.
11 J Garcia Back-Office 15000.00
14.
12 T Clerk Back-Office 10000.00
Expected Results
1.
EmployeeID EmployeeName Department Salary
2.
----------- --------------- --------------- ---------
3.
10 S Martin Back-Office 15000.00
4.
11 J Garcia Back-Office 15000.00
5.
2 D Michael Finance 25000.00
6.
3 A Smith Finance 25000.00
7.
7 J Miller IT 50000.00
8.
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 ...
随机推荐
- 【HDU2222】Keywords Search(AC自动机)
Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...
- VMware Workstation 不可恢复错误: (vcpu-0)
- Spring Batch系列总括(转载)
最近一个项目在使用SpringBatch框架做一个电子商务平台的批处理.网上资料很有限,尤其是中文资料更是少之又少,官网上的文档也只是讲一些入门的基础知识,大部分高级特性都是一笔带过,讲解的很不彻底, ...
- SpringMVC控制器接收不了PUT提交的参数的解决方案
摘要: SpringMVC控制器接收不了PUT提交的参数的解决方案 这次改造了下框架,把控制器的API全部REST化,不做不知道,SpringMVC的REST有各种坑让你去跳,顺利绕过它们花了我不少时 ...
- jinfo命令(Java Configuration Info)
jinfo可以输出并修改运行时的java 进程的opts.用处比较简单,用于输出JAVA系统参数及命令行参数.用法是jinfo -opt pid 如:查看2788的MaxPerm大小可以用 jin ...
- ftp 匿名访问设置
为了让ftp可以匿名访问,需要设置/etc/vsftp.conf 的 anonymous_enable=YES. 当然仅仅是这样,还是不可以的,会出现错误: vsftpd: refusing to r ...
- Node.js权威指南 (7) - 实现基于TCP与UDP的数据通信
7.1 使用net模块实现基于TCP的数据通信 / 136 7.1.1 创建TCP服务器 / 136 7.1.2 socket端口对象 / 142 7.1.3 创建TCP客户端 / 151 7.1.4 ...
- 常用myeclipse的快捷键,对菜鸟超有用的
1. ctrl + h ,这个键超有用的,可以搜索当前项目的整个文件,并锁定到改文件的 具体位置.如图: 2.ctrl + o,在java文件内,搜索该文件下的所有方法.如图: ctrl + d ,删 ...
- Delphi调试CGI或ISAPI 转
因为dll文件已驻留内存,可用intrabob进行调试,也可用PWS进行调试,不过要换文件. IntraBob是资深程序员Dr.Bob编写的免费工具软件,用于测试Delphi编写 的CGI/Win ...
- codeforces --- 115A
A. Party time limit per test 3 seconds memory limit per test 256 megabytes input standard input outp ...