当前位置:网站首页>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 .
边栏推荐
- 283.移动零
- Sort out interface performance optimization skills and kill slow code
- 抓包工具charles实践分享
- 【IEEE】自然语言处理与信息检索国际会议(ECNLPIR 2022)
- 栈题目:括号的分数
- Petit guide de construction rapide du bras mécanique (II): application du bras mécanique
- 图解杂项【防止丢失进行存档用的】
- Process and multithreading
- [EI分享] 2022年第六届船舶,海洋与海事工程国际会议(NAOME 2022)
- 常用的第三方ui框架
猜你喜欢

Flink checkPoint和SavePoint

283. move zero

线程运行原理

Flink集群搭建以及企业级yarn集群搭建

Hill sorting graphic explanation + code implementation

leetCode-2221: 数组的三角和

抓包工具charles实践分享

Multithreaded applications - improve efficiency

Record the range of data that MySQL update will lock

Differences among cookies, session, localstorage and sessionstorage
随机推荐
[data analysis data source] coordinates of provinces, cities and administrative regions across the country (including boundary coordinate points and central coordinate points)
What you must know about distributed systems -cap
SSM整合
The difference between static link library and dynamic link library
leetCode-面试题 16.06: 最小差
JMeter接口测试工具基础— 取样器sampler(二)
Web site development, lazy image loading
leetCode-498: 對角線遍曆
Flink集群搭建以及企业级yarn集群搭建
Difference between package type and basic type
Resolved: methods with the same name as their class will not be constructors in
图解杂项【防止丢失进行存档用的】
JMeter interface test tool foundation - sampler (II)
线程池的状态
Younger sister Juan takes you to learn JDBC --- 2-day sprint Day1
Record the range of data that MySQL update will lock
Leetcode interview question 16.06: minimum difference
Multithreaded applications - improve efficiency
[ei sharing] the 6th International Conference on ship, ocean and Maritime Engineering in 2022 (naome 2022)
Leetcode interview question 01.05: primary editing