1 Star 0 Fork 0

逸扬/leetcode-hub-mysql

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lc-1369.test 1.31 KB
一键复制 编辑 原始数据 按行查看 历史
逸扬 提交于 2022-04-27 00:21 +08:00 . 177-178-185-1077-1126-1270-1285-1308-1368-1412
# $1369. 获取最近第二次的活动
# https://leetcode-cn.com/problems/get-the-second-most-recent-activity/
# SQL架构
Create table If Not Exists UserActivity (username varchar(30), activity varchar(30), startDate date, endDate date);
Truncate table UserActivity;
insert into UserActivity (username, activity, startDate, endDate) values ('Alice', 'Travel', '2020-02-12', '2020-02-20');
insert into UserActivity (username, activity, startDate, endDate) values ('Alice', 'Dancing', '2020-02-21', '2020-02-23');
insert into UserActivity (username, activity, startDate, endDate) values ('Alice', 'Travel', '2020-02-24', '2020-02-28');
insert into UserActivity (username, activity, startDate, endDate) values ('Bob', 'Travel', '2020-02-11', '2020-02-18');
# Write your MySQL query statement below
select
tmp.username,
tmp.activity,
tmp.startDate,
tmp.endDate
from
(
select
u.username,
u.activity,
u.startDate,
u.endDate,
count(*) over(partition by u.username) as cnt,
row_number() over(
partition by u.username
order by
u.startDate desc
) as rn
from
UserActivity u
) tmp
where
tmp.cnt = '1'
or tmp.rn = '2';
# clean-up
drop table UserActivity;
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/gdut_yy/leetcode-hub-mysql.git
git@gitee.com:gdut_yy/leetcode-hub-mysql.git
gdut_yy
leetcode-hub-mysql
leetcode-hub-mysql
master

搜索帮助