当前位置:网站首页>数据库每日一题---第22天:最后一次登录
数据库每日一题---第22天:最后一次登录
2022-07-07 21:49:00 【InfoQ】
一、问题描述
Logins+----------------+----------+
| 列名 | 类型 |
+----------------+----------+
| user_id | int |
| time_stamp | datetime |
+----------------+----------+
(user_id, time_stamp) 是这个表的主键。
每一行包含的信息是user_id 这个用户的登录时间。
SQL20202020二、题目要求
样例
输入:
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
三、问题分析
20202020max四、编码实现
select user_id, max(time_stamp) as 'last_stamp'
from Logins
where year(time_stamp)=2020
group by user_id
五、测试结果



边栏推荐
- Debezium series: source code reading snapshot reader
- Unity与WebGL的相爱相杀
- Leetcode94. Middle order traversal of binary trees
- LeetCode707. Design linked list
- XMIND mind mapping software sharing
- Understand the session, cookie and token at one time, and the interview questions are all finalized
- Revit secondary development - operation family documents
- Debezium系列之: 支持在 KILL 命令中使用变量
- CTF exercise
- Install mxnet GPU version
猜你喜欢
苹果在iOS 16中通过'虚拟卡'安全功能进一步进军金融领域

不夸张地说,这是我见过最通俗易懂的,pytest入门基础教程

行测-图形推理-1-汉字类

UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xf9 in position 56: illegal multibyte sequence

Sword finger offer 28 Symmetric binary tree

行测-图形推理-5-一笔画类

Unity FAQ (I) lack of references

How pyGame rotates pictures

新版代挂网站PHP源码+去除授权/支持燃鹅代抽

PHP method of obtaining image information
随机推荐
Revit secondary development - cut view
There is another problem just online... Warm
Leetcode interview question 02.07 Linked list intersection [double pointer]
Yarn开启ACL用户认证之后无法查看Yarn历史任务日志解决办法
Force deduction - question 561 - array splitting I - step by step parsing
苹果在iOS 16中通过'虚拟卡'安全功能进一步进军金融领域
LeetCode203. Remove linked list elements
What is fake sharing after filling the previous hole?
Line test - graphic reasoning -5- one stroke class
Line measurement - graphic reasoning -9- line problem class
【测试面试题】页面很卡的原因分析及解决方案
How to judge whether the input content is "number"
Understand the autograd package in pytorch
Redis官方ORM框架比RedisTemplate更优雅
Revit secondary development - operation family documents
Variables and constants
Debezium系列之:源码阅读之SnapshotReader
LeetCode142. Circular linked list II [two pointers, two methods for judging links in the linked list and finding ring points]
php 记录完整对接腾讯云直播以及im直播群聊 所遇到的坑
C development - interprocess communication - named pipeline