• LeetCode 1126.查询活跃业务


    数据准备

    Create table If Not Exists Events (business_id int, event_type varchar(10), occurences int);
    Truncate table Events;
    insert into Events (business_id, event_type, occurences) values ('1', 'reviews', '7');
    insert into Events (business_id, event_type, occurences) values ('3', 'reviews', '3');
    insert into Events (business_id, event_type, occurences) values ('1', 'ads', '11');
    insert into Events (business_id, event_type, occurences) values ('2', 'ads', '7');
    insert into Events (business_id, event_type, occurences) values ('3', 'ads', '6');
    insert into Events (business_id, event_type, occurences) values ('1', 'page views', '3');
    insert into Events (business_id, event_type, occurences) values ('2', 'page views', '12');
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    需求

    – 写一段 SQL 来查询所有活跃的业务。
    – 如果一个业务的某个事件类型的发生次数大于此事件类型在所有业务中的平均发生次数,
    – 并且该业务至少有两个这样的事件类型,那么该业务就可被看做是活跃业务。

    输入

    在这里插入图片描述

    输出

    with t1 as (
        select event_type,avg(occurences) as avg_type
        from Events
        group by event_type
    ),t2 as (
        select Events.*,t1.avg_type,
           row_number() over (partition by business_id) as rn1
        from Events , t1
        where t1.event_type=Events.event_type and Events.occurences>t1.avg_type
    )
    select business_id
    from t2
    where rn1>=2
    ;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述

  • 相关阅读:
    软件测试需要学多久 ?这篇文章告诉你答案..
    同一台电脑安装两个版本的idea
    计组期末复习---个人版
    C++&QT day9
    IEEE投稿模板下载
    winodos下使用VS2022编译eclipse-paho.mqtt.c并演示简单使用的 demo
    使用easygui制作app
    lua协程
    Java线程的并发工具类
    MySQL8的ONLY_FULL_GROUP_BY SQL模式兼容问题
  • 原文地址:https://blog.csdn.net/weixin_51696882/article/details/132753639