• LeetCode 1159.市场分析2


    数据准备

    Create table If Not Exists Users (user_id int, join_date date, favorite_brand varchar(10));
    Create table If Not Exists Orders (order_id int, order_date date, item_id int, buyer_id int, seller_id int);
    Create table If Not Exists Items (item_id int, item_brand varchar(10));
    Truncate table Users;
    insert into Users (user_id, join_date, favorite_brand) values ('1', '2019-01-01', 'Lenovo');
    insert into Users (user_id, join_date, favorite_brand) values ('2', '2019-02-09', 'Samsung');
    insert into Users (user_id, join_date, favorite_brand) values ('3', '2019-01-19', 'LG');
    insert into Users (user_id, join_date, favorite_brand) values ('4', '2019-05-21', 'HP');
    Truncate table Orders;
    insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('1', '2019-08-01', '4', '1', '2');
    insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('2', '2019-08-02', '2', '1', '3');
    insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('3', '2019-08-03', '3', '2', '3');
    insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('4', '2019-08-04', '1', '4', '2');
    insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('5', '2019-08-04', '1', '3', '4');
    insert into Orders (order_id, order_date, item_id, buyer_id, seller_id) values ('6', '2019-08-05', '2', '2', '4');
    Truncate table Items;
    insert into Items (item_id, item_brand) values ('1', 'Samsung');
    insert into Items (item_id, item_brand) values ('2', 'Lenovo');
    insert into Items (item_id, item_brand) values ('3', 'LG');
    insert into Items (item_id, item_brand) values ('4', 'HP');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    需求

    写一个 SQL 查询确定每一个用户按日期顺序卖出的第二件商品的品牌是否是他们最喜爱的品牌。如果一个用户卖出少于两件商品,查询的结果是 no 。

    输入

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    输出

    with t1 as (
        select u.*,o.*,I.item_brand
    from Users as u
        left join Orders as o on u.user_id=o.seller_id
        left join Items I on o.item_id = I.item_id
    ),t2 as (
        select user_id,favorite_brand,item_brand,
           row_number() over (partition by user_id order by order_date) as rn1
        from t1
    ),t3 as (
        select user_id,
           case
               when rn1=1 then 'no'
               when (rn1=2 and t2.favorite_brand=t2.item_brand) then 'yes'
               else 'no'
           end as 2nd_item_fav_brand
        from t2
    )
    select user_id as seller_id,
           max(2nd_item_fav_brand) as 2nd_item_fav_brand
    from t3
    group by user_id
    ;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    在这里插入图片描述

  • 相关阅读:
    JavaScript的函数
    原型制作的软件 Experience Design mac( XD ) 中文版软件特色
    借助GPU算力编译Android
    面了三十个人,说说我的真实感受
    useLayoutEffect和useEffect的区别
    PMSM中常用的两种坐标变换——Park变换
    云原生Kubernetes:kubectl管理命令
    鼠标点击选取物体错误的问题
    SpringBoot2 常用注解
    微信小程序css溢出显示省略号失败问题(html解析)
  • 原文地址:https://blog.csdn.net/weixin_51696882/article/details/132965988