SQL实战训练之,力扣:2020. 无流量的帐户数(递归)

目录

一、力扣原题链接

二、题目描述

三、建表语句

四、题目分析

五、SQL解答

六、最终答案

七、验证

八、知识点


一、力扣原题链接

2020. 无流量的帐户数

二、题目描述

表: Subscriptions

复制代码
+-------------+------+
| Column Name | Type |
+-------------+------+
| account_id  | int  |
| start_date  | date |
| end_date    | date |
+-------------+------+
account_id 是此表的主键列。
此表的每一行都表示帐户订阅的开始和结束日期。
请注意,始终开始日期 < 结束日期。

表: Streams

复制代码
+-------------+------+
| Column Name | Type |
+-------------+------+
| session_id  | int  |
| account_id  | int  |
| stream_date | date |
+-------------+------+
session_id是该表的主键列。
account_id是订阅表中的外键。
此表的每一行都包含与会话相关联的帐户和日期的信息。

编写SQL查询以报告在 2021 购买订阅但没有任何会话的帐 户数。

查询结果格式如下例所示。

示例1:

复制代码
输入: 
Subscriptions table:
+------------+------------+------------+
| account_id | start_date | end_date   |
+------------+------------+------------+
| 9          | 2020-02-18 | 2021-10-30 |
| 3          | 2021-09-21 | 2021-11-13 |
| 11         | 2020-02-28 | 2020-08-18 |
| 13         | 2021-04-20 | 2021-09-22 |
| 4          | 2020-10-26 | 2021-05-08 |
| 5          | 2020-09-11 | 2021-01-17 |
+------------+------------+------------+
Streams table:
+------------+------------+-------------+
| session_id | account_id | stream_date |
+------------+------------+-------------+
| 14         | 9          | 2020-05-16  |
| 16         | 3          | 2021-10-27  |
| 18         | 11         | 2020-04-29  |
| 17         | 13         | 2021-08-08  |
| 19         | 4          | 2020-12-31  |
| 13         | 5          | 2021-01-05  |
+------------+------------+-------------+
输出: 
+----------------+
| accounts_count |
+----------------+
| 2              |
+----------------+
解释:用户 4 和 9 在 2021 没有会话。
用户 11 在 2021 没有订阅。

三、建表语句

sql 复制代码
drop table if exists  Subscriptions;
drop table if exists  Streams;
Create table If Not Exists Subscriptions (account_id int, start_date date, end_date date);
Create table If Not Exists Streams (session_id int, account_id int, stream_date date);
Truncate table Subscriptions;
insert into Subscriptions (account_id, start_date, end_date) values ('9', '2020-02-18', '2021-10-30');
insert into Subscriptions (account_id, start_date, end_date) values ('3', '2021-09-21', '2021-11-13');
insert into Subscriptions (account_id, start_date, end_date) values ('11', '2020-02-28', '2020-08-18');
insert into Subscriptions (account_id, start_date, end_date) values ('13', '2021-04-20', '2021-09-22');
insert into Subscriptions (account_id, start_date, end_date) values ('4', '2020-10-26', '2021-05-08');
insert into Subscriptions (account_id, start_date, end_date) values ('5', '2020-09-11', '2021-01-17');
Truncate table Streams;
insert into Streams (session_id, account_id, stream_date) values ('14', '9', '2020-05-16');
insert into Streams (session_id, account_id, stream_date) values ('16', '3', '2021-10-27');
insert into Streams (session_id, account_id, stream_date) values ('18', '11', '2020-04-29');
insert into Streams (session_id, account_id, stream_date) values ('17', '13', '2021-08-08');
insert into Streams (session_id, account_id, stream_date) values ('19', '4', '2020-12-31');
insert into Streams (session_id, account_id, stream_date) values ('13', '5', '2021-01-05');

四、题目分析

需求:

在 2021 购买订阅但没有任何会话的帐户数

解题:

1、在2021订阅中

购买订阅的日期是一个时间段,中间有可能跨年,先提取开始和结束的年,用递归列出所有的年份,筛选出2021年的账户

2、在2021没有任何会话

子查询not in查询会话表中在2021的用户就是在2021年没有会话的用户

3、最后通过筛选出来的数据统计即可

五、SQL解答

六、最终答案

sql 复制代码
with recursive t1 as (
    select account_id,year(start_date)as `year` from Subscriptions
    union all
    select t1.account_id,year + 1
    from t1
    join Subscriptions s1
        on t1.account_id = s1.account_id
        and t1.year < year(s1.end_date)
)
select
    count(distinct account_id) as accounts_count
from t1
-- 在2021年订阅表中
where t1.year = 2021
            -- 不再在2021年消费表中
 and account_id not in (select account_id from Streams where year(stream_date) = 2021)

七、验证

相关推荐
zxrhhm2 分钟前
Oracle 索引完整指南
数据库·oracle
程序猿乐锅1 小时前
【Tilas|第三篇】多表SQL语句
数据库·经验分享·笔记·学习·mysql
Navicat中国2 小时前
使用 Navicat 导入向导导入 Excel 数据时,系统提示导入成功,表中也能看到数据,但行数统计显示为 0,这是什么原因?
数据库·excel·导入
gmaajt2 小时前
Golang怎么做国际化多语言_Golang i18n教程【核心】
jvm·数据库·python
折哥的程序人生 · 物流技术专研2 小时前
从“卡死”到“秒过”:WMS销售数据跨库回填的极限优化之旅
数据库·机器学习·oracle
李可以量化2 小时前
DeepSeek 量化交易实战:用标准化提示词模板实现 AI 辅助交易决策
大数据·数据库·人工智能
maqr_1102 小时前
CSS如何利用Sass定义全局阴影方案_通过变量实现统一CSS风格
jvm·数据库·python
脱氧核糖核酸__2 小时前
LeetCode热题100——234.回文链表(两种解法)
c++·算法·leetcode·链表
m0_613856292 小时前
uni-app怎么做类似于美团的商家评价星级 uni-app五星评分组件制作【实战】
jvm·数据库·python
生信研究猿3 小时前
leetcode 416. 分割等和子集
算法·leetcode·职场和发展