当前位置:网站首页>数据库每日一题---第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
五、测试结果



边栏推荐
- Visual studio 2019 installation
- Knowledge drop - PCB manufacturing process flow
- Revit secondary development - intercept project error / warning pop-up
- 苹果在iOS 16中通过'虚拟卡'安全功能进一步进军金融领域
- Variables and constants
- LeetCode142. Circular linked list II [two pointers, two methods for judging links in the linked list and finding ring points]
- Why is network i/o blocked?
- Early childhood education industry of "screwing bar": trillion market, difficult to be a giant
- 0-5VAC转4-20mA交流电流隔离变送器/转换模块
- Pyqt GUI interface and logic separation
猜你喜欢

数字化转型:五个步骤推动企业进步

Leetcode1984. Minimum difference in student scores

What is fake sharing after filling the previous hole?

UWA Q & a collection

消费品企业敏捷创新转型案例

今日创见|企业促进创新的5大关键要素

安踏DTC | 安踏转型,构建不只有FILA的增长飞轮

Why is network i/o blocked?

Force deduction - question 561 - array splitting I - step by step parsing

行测-图形推理-7-相异图形类
随机推荐
Unity technical notes (II) basic functions of scriptableobject
苹果在iOS 16中通过'虚拟卡'安全功能进一步进军金融领域
新版代挂网站PHP源码+去除授权/支持燃鹅代抽
Basic knowledge of binary tree
今日创见|企业促进创新的5大关键要素
Revit secondary development - collision detection
行测-图形推理-5-一笔画类
Unity 动态合并网格纹理
Line measurement - graphic reasoning -9- line problem class
UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xf9 in position 56: illegal multibyte sequence
Unity and webgl love each other
Sword finger offer 63 Maximum profit of stock
LeetCode144. Preorder traversal of binary tree
php 获取图片信息的方法
Revit secondary development - intercept project error / warning pop-up
Apple further entered the financial sector through the 'virtual card' security function in IOS 16
Debezium系列之:支持 mysql8 的 set role 語句
Amesim2016 and matlab2017b joint simulation environment construction
Online interview, how to better express yourself? In this way, the passing rate will be increased by 50%~
全面掌控!打造智慧城市建设的“领导驾驶舱”