• 2020. 无流量的帐户数


    SQL架构

    表: 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 没有订阅。
    1. with t1 as (select
    2. account_id,start_date,end_date
    3. from
    4. Subscriptions
    5. where year(end_date)=2021
    6. )
    7. select
    8. count(distinct t1.account_id) accounts_count
    9. from
    10. t1 left join Streams s# 是大于等于 和小于等于 不要忘记等于号 要记住
    11. on t1.account_id = s.account_id and t1.start_date <= s.stream_date and t1.end_date >= s.stream_date and year(s.stream_date)=2021
    12. where s.session_id is null

  • 相关阅读:
    考pmp有用么?
    【星海出品】flask (二) request替代VUE测试flask接口
    [附源码]Python计算机毕业设计Django校园快递柜存取件系统
    koa中使用koa-body获取post请求中的参数
    11月7日 mybatis缓存
    Weblogic SSRF 漏洞复现
    基于JAVA在线辅导答疑系统计算机毕业设计源码+系统+数据库+lw文档+部署
    nginx —— win下配置nginx开机自启
    centos免密登录
    鸿蒙HarmonyOS实战-Stage模型(卡片数据交互)
  • 原文地址:https://blog.csdn.net/m0_69157845/article/details/125612542