当前位置:网站首页>The latest entry date of SQL Sever test questions
The latest entry date of SQL Sever test questions
2022-06-24 10:40:00 【Cpsu】
There is such a line on the buckle SQL Ask for the latest entry date . Here, simply create a table .
CREATE TABLE employees
(name VARCHAR(10) NOT NULL,
in_date date NOT NULL)
--indate Indicates the date of employment
INSERT INTO employees
VALUES('BOB','2019-11-20'),
('JAM','2020-01-02'),
('MING','2020-02-02'),
('XIA','2020-02-25'),
('AIM','2020-02-25')
When confirming that there is no repetition of the entry date ,mysql It can be used limit function
SELECT name , in_date FROM employees
ORDER BY in_date DESC LIMIT 0,1
If there is the same latest entry date , The common way of writing can be as follows
SELECT name , in_date FROM employees
WHERE in_date = SELECT MAX(in_date)
FROM employees
however SQL Sever It doesn't seem to support limit function , Only support top. Then the above question can also be rewritten as ( Again, without repetition )
SELECT TOP 1 * FROM employees ORDER BY in_date DESC
Of course, subqueries can also be used in general situations , Rewrite into
SELECT * FROM employees
WHERE in_date = (SELECT TOP 1 in_date
FROM employees
ORDER BY in_date desc)
Here we are expanding , If you want to get the information of the last but three employees ?mysql Very convenient to use limit
SELECT name , in_date FROM employees
ORDER BY in_date DESC LIMIT 2,1
but sql I won't support it , therefore sql Implementation may be a little more cumbersome , The first one can be used top, First, take out the information of the last three in reverse order , Reuse top The one who ranks first in the positive order is the last but three night of employment . The expression here may not be clear , You can look directly at the code .
SELECT TOP 1 *
FROM (SELECT TOP 3 *
FROM employees
ORDER BY in_date DESC) t
ORDER BY in_date
The second one can use window functions .rank(),dense_rank(),row_number() For the difference between the three rankings, please refer to the article
https://blog.csdn.net/m0_46412065/article/details/104951592
The code is as follows
SELECT name,in_date
FROM (SELECT name,in_date,
DENSE_RANK()OVER(ORDER BY in_date DESC) as req
FROM employees) t
WHERE req=3
Finally, I would like to add , The second method is more general , With or without duplicate data , And high expansibility . If you will dense_rank Change it to row_number You can get any row , For example, take the penultimate 8 That's ok , Try it on your own .
边栏推荐
- [JS reverse sharing] community information of a website
- Spark submission parameter -- use of files
- SSM integration
- 2. login and exit function development
- charles抓包工具使用教程
- JMeter接口测试工具基础— 使用Badboy录制JMeter脚本
- Quick completion guide for mechanical arm (II): application of mechanical arm
- 线程池的状态
- Appium automation test foundation - mobile end test environment construction (I)
- 3. addition, deletion, modification and query of employees
猜你喜欢

5. dish management business development

【资源分享】2022年环境工程与生物技术国际会议(CoEEB 2022)

JMeter interface test tool foundation - sampler (II)

百度网盘下载一直请求中问题解决

Customize the toolbars of the kindeditor editor. Items removes unnecessary toolbars or retains some toolbars

24. 图像拼接大作业

JMeter接口测试工具基础— 取样器sampler(二)

希尔排序图文详解+代码实现

Practice sharing of packet capturing tool Charles

2022全网最全最细的jmeter接口测试教程以及接口测试流程详解— JMeter测试计划元件(线程<用户>)
随机推荐
进程与多线程
js数组求和的5种方法
Web项目部署
Leetcode-1823: find the winner of the game
Common methods of thread scheduling
Cookie 、Session、localstorage、Sessionstorage的区别
JMeter interface test tool foundation - use badboy to record JMeter script
charles抓包工具使用教程
机械臂速成小指南(一):机械臂发展概况
The difference between static link library and dynamic link library
126. 单词接龙 II BFS
Leetcode interview question 01.05: primary editing
希尔排序图文详解+代码实现
机械臂速成小指南(零):指南主要内容及分析方法
Flink集群搭建以及企业级yarn集群搭建
Wechat applet rich text picture width height adaptive method introduction (rich text)
Learning to organize using kindeditor rich text editor in PHP
leetCode-2221: 数组的三角和
A method to solve the self-adaptive width and height of the internal picture of rich text label in wechat applet
Illustration miscellaneous [for archiving to prevent loss]