当前位置:网站首页>SQL sever试题求最晚入职日期
SQL sever试题求最晚入职日期
2022-06-24 09:40:00 【Cpsu】
力扣上有这么一道SQL试题求最晚的入职日期。这里简单创建一个表。
CREATE TABLE employees
(name VARCHAR(10) NOT NULL,
in_date date NOT NULL)
--indate表示入职日期
INSERT INTO employees
VALUES('BOB','2019-11-20'),
('JAM','2020-01-02'),
('MING','2020-02-02'),
('XIA','2020-02-25'),
('AIM','2020-02-25')
在确认入职日期没有重复时,mysql可以用 limit 函数
SELECT name , in_date FROM employees
ORDER BY in_date DESC LIMIT 0,1
如果有相同的最晚入职日期,通用写法可以这样写
SELECT name , in_date FROM employees
WHERE in_date = SELECT MAX(in_date)
FROM employees
但是SQL Sever目前好像还不支持limit函数,只支持top。那么对于上述问题也可以改写为(同样是在没有重复的情况下)
SELECT TOP 1 * FROM employees ORDER BY in_date DESC
当然通用情况下也可以使用子查询,改写成
SELECT * FROM employees
WHERE in_date = (SELECT TOP 1 in_date
FROM employees
ORDER BY in_date desc)
这里在拓展一下,如果是要取倒数第三入职的信息呢?mysql很方便可以使用limit
SELECT name , in_date FROM employees
ORDER BY in_date DESC LIMIT 2,1
但sql不支持,所以sql实现可能要麻烦一些,第一种可以使用top,首先倒序取出倒数后三名的信息,再用top取出正序排名第一的就是入职倒数第三晚的。这里表述可能不清,可以直接看代码。
SELECT TOP 1 *
FROM (SELECT TOP 3 *
FROM employees
ORDER BY in_date DESC) t
ORDER BY in_date
第二种可以用到窗口函数。rank(),dense_rank(),row_number()关于这三者排序的区别可以参考文章
https://blog.csdn.net/m0_46412065/article/details/104951592
代码如下
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
最后补充一下,第二种方法比较通用,不管有没有重复数据都可行,且拓展性较高。如果将dense_rank改为row_number则可以实现任意取行,比如取倒数第8行,可以自行尝试。
边栏推荐
- Impdp leading schema message ora-31625 exception handling
- canvas 绘制图片
- Endgame P.O.O
- el-table表格的拖拽 sortablejs
- Recursive traversal of 414 binary tree
- canvas管道动画js特效
- Jcim | AI based protein structure prediction in drug discovery: impacts and challenges
- el-table点击添加行样式
- Latex formula and table recognition
- Is there a reliable and low commission futures account opening channel in China? Is it safe to open an account online?
猜你喜欢

Practical analysis: implementation principle of APP scanning code landing (app+ detailed logic on the web side) with source code

操作符详解

Use of vim

一群骷髅在飞canvas动画js特效

Queue queue

Latex formula and table recognition

Three ways to use applicationcontextinitializer

ByteDance Interviewer: talk about the principle of audio and video synchronization. Can audio and video be absolutely synchronized?

415 binary tree (144. preorder traversal of binary tree, 145. postorder traversal of binary tree, 94. inorder traversal of binary tree)

Indexeddb local storage, homepage optimization
随机推荐
JCIM|药物发现中基于AI的蛋白质结构预测:影响和挑战
100 GIS practical application cases (XIV) -arcgis attribute connection and using Excel
请问有国内靠谱低手续费的期货开户渠道吗?网上开户安全吗?
YOLOv6:又快又准的目标检测框架开源啦
el-table表格的拖拽 sortablejs
SQL statistics of users logged in for N consecutive days
nVisual数字基础设施运营管理软件平台
如何提高网络基础设施排障效率,告别数据断档?
416-二叉树(前中后序遍历—迭代法)
引擎国产化适配&重构笔记
桌面软件开发框架大赏
Symbol. Iterator iterator
Ora-28000 error after upgrading Oracle 12C to 19C
五心红娘
Five heart matchmaker
Arbre binaire partie 1
In depth study paper reading target detection (VII) Chinese English Bilingual Edition: yolov4 optimal speed and accuracy of object detection
PostgreSQL
About thinkphp5, use the model save() to update the data prompt method not exist:think\db\query- & gt; Error reporting solution
涂鸦智能携多款重磅智能照明解决方案,亮相2022美国国际照明展