当前位置:网站首页>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 .
边栏推荐
- Uniapp implements the function of clicking to make a call
- 24. image mosaic operation
- [resource sharing] the 5th International Conference on civil, architectural and environmental engineering in 2022 (iccaee 2022)
- 5. dish management business development
- leetCode-面试题 16.06: 最小差
- Younger sister Juan takes you to learn JDBC --- 2-day sprint Day1
- Flink checkPoint和SavePoint
- 栈题目:括号的分数
- 2022 the most complete and detailed JMeter interface test tutorial and detailed interface test process in the whole network - JMeter test plan component (thread < user >)
- 2022年智能机器人与系统国际研讨会(ISoIRS 2022)
猜你喜欢
随机推荐
程序员在技术之外,还要掌握一个技能——自我营销能力
Role of message queuing
[resource sharing] 2022 International Conference on Environmental Engineering and Biotechnology (coeeb 2022)
Learn how to use PHP to filter special symbols in strings
线程的六种状态
机械臂速成小指南(零):指南主要内容及分析方法
Appium automation test foundation - mobile end test environment construction (I)
SF Technology Smart logistics Campus Technology Challenge (June 19, 2022) [AK]
【IEEE出版】2022年工业自动化,机器人与控制工程国际会议(IARCE 2022)
Multithreaded applications - improve efficiency
The difference between the sleep () method and the wait () method of a thread
What you must know about distributed systems -cap
numpy. linspace()
[IEEE publication] 2022 International Conference on intelligent transportation and future travel (cstfm 2022)
Learn to use the phpstripslush function to remove backslashes
[ei sharing] the 6th International Conference on ship, ocean and Maritime Engineering in 2022 (naome 2022)
283. move zero
numpy. logical_ and()
numpy. logical_ or
Leetcode-929: unique email address









