很久以前准备写的系列文章,后来因为懒一直耽搁着,今天突然决定继续下去,于是有了这篇文章,很基础,但很常用。题目描述依然拷贝。简单来说就是找出个个部门薪水排名第二的人,排名相同的要一起列出来。

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

  1. The solution should work on SQL Server 2005 and above.
  2. 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的更多相关文章

  1. TSQL Beginners Challenge 3 - Find the Factorial

    这是一个关于CTE的应用,这里我们用CTE实现阶乘 Factorial,首先来看一个简单的小实验,然后再来看题目.有的童鞋会问怎么没有2就来3了呢,惭愧,TSQL Beginners Challeng ...

  2. 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. +----+ ...

  3. [LeetCode] Department Highest Salary 系里最高薪水

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...

  4. [LeetCode] Nth Highest Salary 第N高薪水

    Write a SQL query to get the nth highest salary from the Employee table. +----+--------+ | Id | Sala ...

  5. [LeetCode] Second Highest Salary 第二高薪水

    Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | S ...

  6. 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 ...

  7. Leetcode 176. Second Highest Salary

    Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | S ...

  8. find the Nth highest salary(寻找第N高薪水)

    Suppose that you are given the following simple database table called Employee that has 2 columns na ...

  9. [SQL]LeetCode176. 第二高的薪水 | Second Highest Salary

    Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | S ...

随机推荐

  1. Android 使用HttpClient方式提交POST请求

    final String username = usernameEditText.getText().toString().trim(); final String password = passwr ...

  2. Android开源项目发现--- 安全篇(持续更新)

    SQLCipher Sqlite加密工具 项目地址:https://github.com/sqlcipher/sqlcipher 帮助文档:http://sqlcipher.net/sqlcipher ...

  3. Android AlertDialog更改标题颜色,字体等

    更改AlertDialog标题的方法google目前没有提供,只能通过其他办法 一种办法是:首先在源代码中找到有个叫AlertController的类,这个类就是AlertDialog的实现类,是没有 ...

  4. Java随机数

    本章先讲解Java随机数的几种产生方式,然后通过示例对其进行演示. 广义上讲,Java中的随机数的有三种产生方式:(01). 通过System.currentTimeMillis()来获取一个当前时间 ...

  5. jps命令(Java Virtual Machine Process Status Tool)

    1.介绍 用来查看基于HotSpot的JVM里面中,所有具有访问权限的Java进程的具体状态, 包括进程ID,进程启动的路径及启动参数等等,与unix上的ps类似,只不过jps是用来显示java进程, ...

  6. 解决Mac OS X Lion狮子系统及win7多分区教程

    [绿茶教程]解决Mac OS X Lion狮子系统及win7多分区教程   工具/原料 8G的u盘制作lion系统安装盘   步骤/方法  插入U盘---开机---按住左下角“Option”键(alt ...

  7. SRM 408(1-250pt, 1-500pt)

    DIV1 250pt 题意:每天晚上需要点蜡烛,且每晚蜡烛燃烧1cm,第i天晚上需要点i根蜡烛.第一天白天的时候,拥有一些蜡烛,用vector<int>can表示他们的长度,问最多能烧几个 ...

  8. 计算1到n整数中,字符ch出现的次数

    个位ch个数 + 十位ch个数 * 10 + 百位ch个数 * 100:同时如果某一位刚好等于ch,还需要减去多算的一部分值. #include <stdio.h> //整数1到n,字符c ...

  9. vim setting

    django_百度搜索 最近合并代码,发现文件缩进经常不一致,请大家把以下配置放到自己主目录下.vimrc文件中.   set tabstop=4   set shiftwidth=4   set e ...

  10. 关于日历控件My97DatePicker 在IE6下出现“无法打开站点,已终止操作”

    1.My97DatePicker 官方:http://www.my97.net2.在IE6下出现“无法打开站点,已终止操作”的解决办法(转): .这是一个绝对有效的方法,但是会丢失跨越iframe的特 ...