当前位置:网站首页>数据库每日一题---第22天:最后一次登录
数据库每日一题---第22天:最后一次登录
2022-07-07 21:49:00 【InfoQ】
一、问题描述
Logins
+----------------+----------+
| 列名 | 类型 |
+----------------+----------+
| user_id | int |
| time_stamp | datetime |
+----------------+----------+
(user_id, time_stamp) 是这个表的主键。
每一行包含的信息是user_id 这个用户的登录时间。
SQL
2020
2020
二、题目要求
样例
输入:
Logins 表:
+---------+---------------------+
| user_id | time_stamp |
+---------+---------------------+
| 6 | 2020-06-30 15:06:07 |
| 6 | 2021-04-21 14:06:06 |
| 6 | 2019-03-07 00:18:15 |
| 8 | 2020-02-01 05:10:53 |
| 8 | 2020-12-30 00:46:50 |
| 2 | 2020-01-16 02:49:50 |
| 2 | 2019-08-25 07:59:08 |
| 14 | 2019-07-14 09:00:00 |
| 14 | 2021-01-06 11:59:59 |
+---------+---------------------+
输出:
+---------+---------------------+
| user_id | last_stamp |
+---------+---------------------+
| 6 | 2020-06-30 15:06:07 |
| 8 | 2020-12-30 00:46:50 |
| 2 | 2020-01-16 02:49:50 |
+---------+---------------------+
解释:
6号用户登录了3次,但是在2020年仅有一次,所以结果集应包含此次登录。
8号用户在2020年登录了2次,一次在2月,一次在12月,所以,结果集应该包含12月的这次登录。
2号用户登录了2次,但是在2020年仅有一次,所以结果集应包含此次登录。
14号用户在2020年没有登录,所以结果集不应包含。
考察
1.聚合函数
2.建议用时10~25min
三、问题分析
2020
2020
max
四、编码实现
select user_id, max(time_stamp) as 'last_stamp'
from Logins
where year(time_stamp)=2020
group by user_id
五、测试结果



边栏推荐
- Failed to initialize rosdep after installing ROS
- Nx10.0 installation tutorial
- Redis official ORM framework is more elegant than redistemplate
- 微生物健康网,如何恢复微生物群落
- Visual studio 2019 installation
- 数字化转型:五个步骤推动企业进步
- Revit secondary development - modify wall thickness
- LeetCode142. Circular linked list II [two pointers, two methods for judging links in the linked list and finding ring points]
- Gazebo import the mapping model created by blender
- Details of the open source framework of microservice architecture
猜你喜欢
Understand the session, cookie and token at one time, and the interview questions are all finalized
LeetCode144. Preorder traversal of binary tree
There is another problem just online... Warm
Sword finger offer 28 Symmetric binary tree
Personal statement of testers from Shuangfei large factory: is education important for testers?
Nx10.0 installation tutorial
Line test - graphic reasoning - 2 - black and white lattice class
Line test - graphic reasoning - 3 - symmetric graphic class
数字藏品加速出圈,MarsNFT助力多元化文旅经济!
微生物健康網,如何恢複微生物群落
随机推荐
Line test - graphic reasoning - 2 - black and white lattice class
ASP.NET Core入门五
数字藏品加速出圈,MarsNFT助力多元化文旅经济!
Debezium series: binlogreader for source code reading
IP network active evaluation system -- x-vision
Details of the open source framework of microservice architecture
行测-图形推理-7-相异图形类
Line test - graphic reasoning -7- different graphic classes
Explain in detail the communication mode between arm A7 and risc-v e907 on Quanzhi v853
UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xf9 in position 56: illegal multibyte sequence
行测-图形推理-3-对称图形类
Signal feature extraction +lstm to realize gear reducer fault diagnosis -matlab code
Leetcode interview question 02.07 Linked list intersection [double pointer]
Force deduction - question 561 - array splitting I - step by step parsing
Line test - graphic reasoning - 3 - symmetric graphic class
Ren Qian code compilation error modification
Debezium series: source code reading snapshot reader
How to judge whether the input content is "number"
Matplotlib quick start
Digital transformation: five steps to promote enterprise progress