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)

七、验证

相关推荐
兰令水4 分钟前
hot100【acm版】【2026.7.21打卡-java版本】
java·开发语言·算法·leetcode·面试
小大宇7 分钟前
mongoDB dump技巧
数据库·mongodb
huijingjituan10 分钟前
三方聊天软件工具定制开发|打造企业级智能通讯平台
数据库·安全·阿里云·实时互动·腾讯云
zyplayer-doc13 分钟前
研发接口文档怎么长期维护:zyplayer-doc把API、Markdown和变更记录放进同一个知识库
大数据·数据库·人工智能·笔记·pdf·ocr
NineData13 分钟前
Oracle 卡住时先看阻塞源,ChatDBA 会先把锁链路理出来
数据库·oracle·ninedata·锁故障·锁等待·chatdba·锁阻塞
Macbethad27 分钟前
基于WPF与.NET的洁净厂房数据孪生平台技术方案:架构、实现与SEMI标准实践
数据库·系统架构
咏方舟【长江支流】1 小时前
【开源】跨语言·跨平台·跨数据库(6) ——一种ORM缓存的接口实现和容错处理
数据库·缓存·开源·咏方舟-长江支流·用宝框架
努力进修1 小时前
破除工业 AI 业务落地壁垒:多模时序融合架构重塑设备全维度数据价值
数据库·人工智能·架构
退休倒计时1 小时前
【每日一题】LeetCode 131. 分割回文串 TypeScript
算法·leetcode·typescript
静水楼台x1 小时前
spring security
java·数据库·spring