HQL刷题 50道

HQL刷题 50道

尚硅谷HQL刷题网站

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

答案

1.查询累积销量排名第二的商品

select sku_id
from (select sku_id, dense_rank() over (order by total desc) rn
      from (select sku_id, sum(sku_num) total
            from order_detail
            group by sku_id) t1) t2
where rn = 2;

2.查询至少连续三天下单的用户

select user_id
from (select user_id,
             create_date,
             sum(if(diff > 1, 1, 0)) over (partition by user_id order by create_date) groups
      from (select user_id,
          create_date,
          yestoday,
          datediff(create_date, yestoday) diff from (select user_id,
          create_date,
          lead (create_date, 1, '1970-01-01') over (partition by user_id order by create_date) as yestoday from
          (select user_id, create_date
          from order_info
          group by user_id, create_date)
          t1) t2) t3) t4
group by user_id, groups
having count (*)>=3;

3.查询各品类销售商品的种类数及销量最高的商品

select t3.category_id,
       cate.category_name,
       t3.sku_id,
       t3.name,
       t3.order_num,
       t3.sku_cnt
from (select category_id,
             sku_id,
             name,
             order_num,
             sku_cnt
      from (select category_id,
                   sku_id,
                   name,
                   order_num,
                   count(distinct sku_id) over (partition by category_id)               sku_cnt,
                   row_number() over (partition by category_id order by order_num desc) rn
            from (select sku.category_id, sku.sku_id, sku.name, sum(od.sku_num) order_num
                  from order_detail od
                           join sku_info sku on od.sku_id = sku.sku_id
                  group by sku.category_id, sku.sku_id, sku.name) t1) t2
      where rn = 1) t3
         join category_info cate on t3.category_id = cate.category_id;

4 查询用户的累计消费金额及VIP等级

select user_id,
       create_date,
       sum_so_far,
       case
           when sum_so_far >= 0 and sum_so_far < 10000 then '普通会员'
           when sum_so_far >= 10000 and sum_so_far < 30000 then '青铜会员'
           when sum_so_far >= 30000 and sum_so_far < 50000 then '白银会员'
           when sum_so_far >= 50000 and sum_so_far < 80000 then '黄金会员'
           when sum_so_far >= 80000 and sum_so_far < 100000 then '白金会员'
           when sum_so_far >= 100000 then '钻石会员' end vip_level
from (select user_id,
             create_date,
             sum(total) over (partition by user_id order by create_date) sum_so_far
      from (select user_id, create_date, sum(total_amount) total from order_info group by user_id, create_date) t1
      order by user_id, create_date) t2;

5 查询首次下单后第二天连续下单的用户比率

select concat(round(users * 1.0 / total * 100, 1), '%') as percentage
from (select count(distinct user_id) users, total
      from (select user_id,
                   create_date,
                   first_value(create_date) over (partition by user_id order by create_date) first_day,
                   count(distinct user_id) over ()                                           total
            from order_info) t1
      where datediff(create_date, first_day) = 1
      group by total) t2;

6 每个商品销售首年的年份、销售数量和销售金额

select sku_id, year, order_num, order_amount
from (select sku_id, year, order_num, order_amount, row_number() over (partition by sku_id order by year) rn
      from (select sku_id, year(create_date) year, sum(sku_num) order_num, sum(sku_num * price) order_amount
            from order_detail
            group by sku_id, year(create_date)) t1) t2
where rn = 1;

7 筛选去年总销量小于100的商品

select sku_id, name, order_num
from (select sku.sku_id, sku.name, sum(sku_num) order_num
      from order_detail od
               join sku_info sku on sku.sku_id = od.sku_id
      where year(od.create_date) = '2021'
        and datediff(od.create_date, sku.from_date) >= 30
      group by sku.sku_id, sku.name) t1
where order_num < 100;

8 查询每日新用户数

select login_date_first, count(distinct user_id) user_count
from (select user_id,
             to_date(login_ts)                                          login_date_first,
             row_number() over (partition by user_id order by login_ts) rn
      from user_login_detail) t1
where rn = 1
group by login_date_first;

9 统计每个商品的销量最高的日期

select sku_id,
       create_date,
       sum_num
from (select sku_id,
             create_date,
             sum_num,
             row_number() over (partition by sku_id order by sum_num desc,create_date) rn
      from (select sku_id, create_date, sum(sku_num) sum_num
            from order_detail
            group by sku_id, create_date) t1) t2
where rn = 1
   or create_date = current_date();

10 查询销售件数高于品类平均数的商品

select sku_id,
       name,
       sum_num,
       floor(avg_num) cate_avg_num
from (select sku_id,
             name,
             sum_num,
             avg(sum_num) over (partition by category_id) avg_num
      from (select sku.sku_id,
                   sku.name,
                   sku.category_id,
                   sum(od.sku_num) sum_num
            from order_detail od
                     join sku_info sku
                          on od.sku_id = sku.sku_id
            group by sku.sku_id, sku.name, sku.category_id) t1) t2
where sum_num > floor(avg_num);

11 用户注册、登录、下单综合统计

select t1.user_id,
       to_date(t1.register_time) register_date,
       t1.total_login_count,
       t1.login_count_2021,
       count(*)                  order_count_2021,
       sum(od.total_amount)      order_amount_2021
from (select distinct user_id,
                      first_value(login_ts) over (partition by user_id order by login_ts) register_time,
                      count(*) over (partition by user_id)                                total_login_count,
                      sum(if(year(login_ts) = '2021', 1, 0)) over (partition by user_id)  login_count_2021
      from user_login_detail) t1
         join order_info od on od.user_id = t1.user_id
where year(od.create_date) = '2021'
group by t1.user_id, to_date(t1.register_time),
         t1.total_login_count,
         t1.login_count_2021;

12 查询指定日期的全部商品价格

select sku_id,
       price
from (select sku_id,
             cast(tmp_price as decimal(16, 2))                        price,
             row_number() over (partition by sku_id order by dt desc) rn
      from (select t1.sku_id, nvl(t2.change_date, t1.from_date) dt, nvl(t2.new_price, t1.price) tmp_price
            from (select sku_id, price, from_date
                  from sku_info
                  where from_date <= '2021-10-01') t1
                     left join
                 (select sku_id, new_price, change_date
                  from sku_price_modify_detail
                  where change_date <= '2021-10-01') t2 on t1.sku_id = t2.sku_id) t3) t4
where rn = 1;

13 即时订单比例

select cast(plan / total as decimal(16, 2)) percentage
from (select count(*) total, sum(if(order_date = custom_date, 1, 0)) plan
      from (select user_id,
                   order_date,
                   custom_date,
                   row_number() over (partition by user_id order by order_date) rn
            from delivery_info) t1
      where rn = 1) t2;

14 向用户推荐朋友收藏的商品

select distinct ship.user1_id user_id, f1.sku_id
from friendship_info ship
         join favor_info f1 on ship.user2_id = f1.user_id
         left join favor_info f2 on f2.user_id = ship.user1_id and f2.sku_id = f1.sku_id
where f2.sku_id is null;

select user_id, sku_id
from (select distinct ship.user1_id user_id, f1.sku_id
      from friendship_info ship
               join favor_info f1 on ship.user2_id = f1.user_id
      union all
      select user_id, sku_id
      from favor_info) t1
group by user_id, sku_id
having count(*) < 2;

15 查询所有用户的连续登录两天及以上的日期区间

select user_id, min(dt) start_date, max(dt) end_date
from (select user_id, dt, sum(if(diff > 1, 1, 0)) over (partition by user_id order by dt) nums
      from (select user_id, dt, datediff(dt, yestoday) diff
            from (select user_id, dt, lag(dt, 1, '1970-01-01') over (partition by user_id order by dt) yestoday
                  from (select user_id, to_date(login_ts) dt
                        from user_login_detail
                        group by user_id, to_date(login_ts)) t1) t2) t3) t4
group by user_id, nums
having count(*) > 1;

16 男性和女性每日的购物总金额统计

select od.create_date,
       sum(if(u.gender = '男', od.total_amount, 0)) total_amount_male,
       sum(if(u.gender = '女', od.total_amount, 0)) total_amount_female
from order_info od
         join user_info u on od.user_id = u.user_id
group by od.create_date;

17 订单金额趋势分析

select create_date,
       round(sum(total_amount) over (order by ts range between 172800 preceding and current row), 2) total_3d,
       round(avg(total_amount) over (order by ts range between 172800 preceding and current row), 2) avg_3d
from (select create_date,
             unix_timestamp(create_date, 'yyyy-MM-dd') ts,
             sum(total_amount)                         total_amount
      from order_info
      group by create_date) t1;

18.购买过商品1和商品2但是没有购买商品3的顾客

select user_id
from (select distinct order_info.user_id, order_detail.sku_id
      from order_info
               join order_detail on order_info.order_id = order_detail.order_id
      where order_detail.sku_id in (1, 2, 3)) t1
group by user_id
having sum(if(sku_id = 3, 3, 1)) = 2;

19 统计每日商品1和商品2销量的差值

select create_date,
       sum(if(sku_id = 1, sku_num, 0)) - sum(if(sku_id = 2, sku_num, 0)) diff
from order_detail
where sku_id in (1, 2)
group by create_date;

20 查询出每个用户的最近三笔订单

select user_id,
       order_id,
       create_date
from (select user_id,
             order_id,
             create_date,
             dense_rank() over (partition by user_id order by create_date desc) rn
      from order_info) t1
where rn < 4;

21 查询每个用户登录日期的最大空档期

select user_id, max(datediff(future, dt)) max_diff
from (select user_id,
             dt,
             lead(dt, 1, '2021-10-10') over (partition by user_id order by dt) future
      from (select distinct user_id, to_date(login_ts) dt from user_login_detail) t1) t2
group by user_id;

22 查询相同时刻多地登陆的用户

select user_id
from (select u1.user_id,
             if(u1.login_ts <= u2.login_ts, if(u1.logout_ts >= u2.login_ts, if(u1.ip_address = u2.ip_address, 0, 1), 0),
                0) num
      from user_login_detail u1
               join user_login_detail u2 on u1.user_id = u2.user_id and u1.login_ts != u2.login_ts) t2
group by user_id
having sum(num) > 0;

23 销售额完成任务指标的商品

select distinct sku_id
from (select sku_id, sum(if(diff > 1, 1, 0)) over (partition by sku_id order by dt) num
      from (select sku_id, dt, (year(dt) - year(pass)) * 12 + month(dt) - month(pass) diff
            from (select sku_id, dt, lag(dt, 1, '1970-01-01') over (partition by sku_id order by dt) pass
                  from (select sku_id, dt
                        from (select sku_id, trunc(create_date, 'MM') dt, sum(price * sku_num) total
                              from order_detail
                              group by sku_id, trunc(create_date, 'MM')) t1
--   按题目的过滤条件 where not ((sku_id = 1 and total < 21000) or (sku_id = 2 and total < 10000))
                        where (sku_id = 1 and total >= 21000)
                           or (sku_id = 2 and total >= 10000)) t2) t3) t4) t5
group by sku_id, num
having count(*) > 1;

24 根据商品销售情况进行商品分类

select category, count(*) cn
from (select sku_id,
             case
                 when total <= 5000 then '冷门商品'
                 when total <= 19999 then '一般商品'
                 else '热门商品' end category
      from (select sku_id, sum(sku_num) total from order_detail group by sku_id) t1) t2
group by category;

25 各品类销量前三的所有商品 题目意思不明确 dense_rank() row_number()

select sku_id,
       category_id
from (select sku_id,
             category_id,
             dense_rank() over (partition by category_id order by total desc) rn
      from (select od.sku_id, sku.category_id, sum(sku_num) total
            from order_detail od
                     join sku_info sku on od.sku_id = sku.sku_id
            group by od.sku_id, sku.category_id) t1) t2
where rn < 4;

26 各品类中商品价格的中位数

select category_id,
       cast(avg(price) as decimal(16, 2)) medprice
from (select category_id,
             price,
             row_number() over (partition by category_id order by price) rn,
             count(*) over (partition by category_id)                    cn
      from sku_info) t1
where rn in (ceil((cn + 1) * 0.5), floor((cn + 1) * 0.5))
group by category_id;

27 找出销售额连续3天超过100的商品

select distinct sku_id
from order_detail
where create_date in
      (select distinct create_date
       from (select create_date, count(*) over (order by nums) cnt
             from (select create_date,
                          sum(if(diff > 1, 1, 0)) over (order by create_date) nums
                   from (select create_date,
                                datediff(create_date,
                                         lag(create_date, 1, '1970-01-01') over (order by create_date)) diff
                         from (select create_date
                               from order_detail
                               group by create_date
                               having sum(sku_num) > 100) t1) t2) t3) t4
       where cnt > 2);

28 查询有新注册用户的当天的新用户数量、新用户的第一天留存率

select first_login,
       sum(if(diff = 0, 1, 0))                                                       register,
       cast((sum(if(diff = 1, 1.0, 0)) / sum(if(diff = 0, 1, 0))) as decimal(16, 2)) retention
from (select distinct user_id,
                      to_date(min(login_ts) over (partition by user_id))                              first_login,
                      datediff(to_date(login_ts), to_date(min(login_ts) over (partition by user_id))) diff
      from user_login_detail) t1
group by first_login;

29 求出商品连续售卖的时间区间

select sku_id,
       min(create_date) start_date,
       max(create_date) end_date
from (select sku_id,
             create_date,
             sum(if(diff > 1, 1, 0)) over (partition by sku_id order by create_date) nums
      from (select sku_id,
                   create_date,
                   datediff(create_date,
                            lag(create_date, 1, '1970-01-01') over (partition by sku_id order by create_date)) diff
            from (select distinct sku_id, create_date
                  from order_detail) t1) t2) t3
group by sku_id, nums;

30 登录次数及交易次数统计

select t1.user_id, t1.login_date, t1.login_count, nvl(t2.order_count, 0) order_count
from (select user_id, to_date(login_ts) login_date, count(*) login_count
      from user_login_detail
      group by user_id, to_date(login_ts)) t1
         left join
     (select user_id, order_date, count(*) order_count
      from delivery_info
      group by user_id, order_date) t2 on t1.user_id = t2.user_id and t1.login_date = t2.order_date;

31 按年度列出每个商品销售总额

select sku_id,
       year(create_date)                            year_date,
       cast(sum(sku_num * price) as decimal(16, 2)) sku_sum
from order_detail
group by sku_id, year(create_date);

32 某周内每件商品每天销售情况

select sku_id,
       sum(if(dayofweek(create_date) = 2, sku_num, 0)) monday,
       sum(if(dayofweek(create_date) = 3, sku_num, 0)) tuesday,
       sum(if(dayofweek(create_date) = 4, sku_num, 0)) wednesday,
       sum(if(dayofweek(create_date) = 5, sku_num, 0)) thursday,
       sum(if(dayofweek(create_date) = 6, sku_num, 0)) friday,
       sum(if(dayofweek(create_date) = 7, sku_num, 0)) saturday,
       sum(if(dayofweek(create_date) = 1, sku_num, 0)) sunday
from order_detail
where create_date >= '2021-09-27'
  and create_date <= '2021-10-03'
group by sku_id;

33 查看每件商品的售价涨幅情况(排除只有1次涨幅的)

select sku_id, price_change
from (select sku_id,
             row_number() over (partition by sku_id order by change_date desc)                        rn,
             count(*) over (partition by sku_id)                                                      cn,
             new_price - (lead(new_price, 1, 0) over (partition by sku_id order by change_date desc)) price_change
      from sku_price_modify_detail) t1
where rn = 1
  and cn > 1;

34 销售订单首购和次购分析

-- 题目实际意思
select user_id, min(create_date) first_date, max(create_date) last_date, cn
from (select user_id,
             create_date,
             cn,
             row_number() over (partition by user_id order by create_date) rn
      from (select o.user_id, o.create_date, count(*) over (partition by o.user_id) cn
            from sku_info sku
                     join order_detail od on sku.sku_id = od.sku_id
                     join order_info o on o.order_id = od.order_id
            where sku.name in ('xiaomi 10', 'apple 12', 'xiaomi 13')) t1
      where cn > 1) t2
where rn < 3
group by user_id, cn;
-- 实际结果
select user_id, min(create_date) first_date, max(create_date) last_date, cn
from (select user_id, create_date, cn
      from (select o.user_id, o.create_date, count(*) over (partition by o.user_id) cn
            from sku_info sku
                     join order_detail od on sku.sku_id = od.sku_id
                     join order_info o on o.order_id = od.order_id
            where sku.name in ('xiaomi 10', 'apple 12', 'xiaomi 13')) t1
      where cn > 1) t2
group by user_id, cn;

35 同期商品售卖分析表

select sku_id,
       month(create_date)                            month,
       sum(if(year(create_date) = 2020, sku_num, 0)) 2020_skusum,
       sum(if(year(create_date) = 2021, sku_num, 0)) 2021_skusum
from order_detail
-- 按题目意思 where create_date >= '2021-01-01' and create_date < '2023-01-01'
where create_date >= '2020-01-01'
  and create_date < '2022-01-01'
group by sku_id, month(create_date);

36 国庆期间每个品类的商品的收藏量和购买量

select t1.sku_id, t1.total sku_sum, nvl(t2.uv, 0) favor_cn
from (select sku_id, sum(sku_num) total
      from order_detail
      where create_date >= '2021-10-01'
        and create_date <= '2021-10-07'
      group by sku_id) t1
         left join
     (select sku_id, count(distinct user_id) uv
      from favor_info
      where create_date >= '2021-10-01'
        and create_date <= '2021-10-07'
      group by sku_id) t2 on t1.sku_id = t2.sku_id;

37 统计活跃间隔对用户分级结果

select level, count(*) cn
from (select case
                 when datediff(today, register) <= 7 then '新增用户'
                 when datediff(today, login) <= 7 then '忠实用户'
                 when datediff(today, login) < 30 then '沉睡用户'
                 else '流失用户' end level
      from (select distinct user_id,
                            max(dt) over ()                     today,
                            min(dt) over (partition by user_id) register,
                            max(dt) over (partition by user_id) login
            from (select distinct user_id, to_date(login_ts) dt
                  from user_login_detail) t1) t2) t3
group by level;

38 连续签到领金币数

select user_id,
       sum(if(total % 7 > 2, floor(total / 7) * 15 + (total % 7) + 2, floor(total / 7) * 15 + (total % 7))) sum_coin_cn
from (select user_id, count(*) total
      from (select user_id,
                   sum(if(nums > 1, 1, 0)) over (partition by user_id order by dt) type
            from (select user_id,
                         dt,
                         datediff(dt, lag(dt, 1, '1970-01-01') over (partition by user_id order by dt)) nums
                  from (select distinct user_id, to_date(login_ts) dt
                        from user_login_detail) t1) t2) t3
      group by user_id, type) t4
group by user_id
order by sum_coin_cn desc;

39 国庆期间的7日动销率和滞销率

-- 固定式(要优化)
select category_id,
       cast(round(first / first_total, 2) as decimal(16, 2))         first_sale_rate,
       cast(1 - round(first / first_total, 2) as decimal(16, 2))     first_unsale_rate,
       cast(round(second / second_total, 2) as decimal(16, 2))       second_sale_rate,
       cast(1 - round(second / second_total, 2) as decimal(16, 2))   second_unsale_rate,
       cast(round(third / third_total, 2) as decimal(16, 2))         third_sale_rate,
       cast(1 - round(third / third_total, 2) as decimal(16, 2))     third_unsale_rate,
       cast(round(fourth / fourth_total, 2) as decimal(16, 2))       fourth_sale_rate,
       cast(1 - round(fourth / fourth_total, 2) as decimal(16, 2))   fourth_unsale_rate,
       cast(round(fifth / fifth_total, 2) as decimal(16, 2))         fifth_sale_rate,
       cast(1 - round(fifth / fifth_total, 2) as decimal(16, 2))     fifth_unsale_rate,
       cast(round(sixth / sixth_total, 2) as decimal(16, 2))         sixth_sale_rate,
       cast(1 - round(sixth / sixth_total, 2) as decimal(16, 2))     sixth_unsale_rate,
       cast(round(seventh / seventh_total, 2) as decimal(16, 2))     seventh_sale_rate,
       cast(1 - round(seventh / seventh_total, 2) as decimal(16, 2)) seventh_unsale_rate
from (select sku.category_id,
             count(distinct if(sku.from_date <= '2021-10-01', sku.sku_id, null)) first_total,
             count(distinct if(od.create_date = '2021-10-01', od.sku_id, null))  first,
             count(distinct if(sku.from_date <= '2021-10-02', sku.sku_id, null)) second_total,
             count(distinct if(od.create_date = '2021-10-02', od.sku_id, null))  second,
             count(distinct if(sku.from_date <= '2021-10-03', sku.sku_id, null)) third_total,
             count(distinct if(od.create_date = '2021-10-03', od.sku_id, null))  third,
             count(distinct if(sku.from_date <= '2021-10-04', sku.sku_id, null)) fourth_total,
             count(distinct if(od.create_date = '2021-10-04', od.sku_id, null))  fourth,
             count(distinct if(sku.from_date <= '2021-10-05', sku.sku_id, null)) fifth_total,
             count(distinct if(od.create_date = '2021-10-05', od.sku_id, null))  fifth,
             count(distinct if(sku.from_date <= '2021-10-06', sku.sku_id, null)) sixth_total,
             count(distinct if(od.create_date = '2021-10-06', od.sku_id, null))  sixth,
             count(distinct if(sku.from_date <= '2021-10-07', sku.sku_id, null)) seventh_total,
             count(distinct if(od.create_date = '2021-10-07', od.sku_id, null))  seventh
      from sku_info sku
               left join order_detail od on sku.sku_id = od.sku_id
      group by sku.category_id) t1;

40 出平台同时在线最多的人数

select max(num) as cn
from (select sum(flag) over (order by dt) num
      from (select login_ts dt, 1 flag
            from user_login_detail
            union all
            select logout_ts dt, -1 flag
            from user_login_detail) t1) t2;

41 同时在线人数问题

select live_id, max(num) max_user_count
from (select live_id, sum(flag) over (partition by live_id order by dt) num
      from (select user_id, live_id, in_datetime dt, 1 flag
            from live_events
            union all
            select user_id, live_id, out_datetime dt, -1 flag
            from live_events) t1) t2
group by live_id;

42 会话划分问题

select user_id,
       page_id,
       view_timestamp,
       concat(user_id, '-', sum(if(diff > 60, 1, 0)) over (partition by user_id order by view_timestamp)) session_id
from (select user_id,
             page_id,
             view_timestamp,
             view_timestamp - lag(view_timestamp, 1, 0) over (partition by user_id order by view_timestamp) diff
      from page_view_events) t1;

43 间断连续登录用户问题

select user_id, max(num) max_day_count
from (select user_id, datediff(max(dt), min(dt)) + 1 num
      from (select user_id, dt, sum(if(diff > 2, 1, 0)) over (partition by user_id order by dt) type
            from (select user_id,
                         dt,
                         datediff(dt, lag(dt, 1, '1970-01-01') over (partition by user_id order by dt)) diff
                  from (select distinct user_id, to_date(login_datetime) dt
                        from login_events) t1) t2) t3
      group by user_id, type) t4
group by user_id;

44 日期交叉问题

select brand, sum(if(datediff(end_date, stt) >= 0, datediff(end_date, stt) + 1, 0)) promotion_day_count
from (select brand,
             if(max_date is null, start_date, if(start_date > max_date, start_date, date_add(max_date, 1))) stt,
             end_date
      from (select brand,
                   start_date,
                   end_date,
                   max(end_date)
                       over (partition by brand order by start_date rows between UNBOUNDED PRECEDING and 1 PRECEDING) max_date
            from promotion_info) t1) t2
group by brand;

45 复购率问题(注意全是90天内)

select product_id, cast(sum(if(nums > 1, 1, 0)) / count(*) as decimal(16, 2)) as cpr
from (select user_id, product_id, count(*) nums
      from (select user_id, product_id, datediff(max(order_date) over (), order_date) diff
            from order_detail) t1
      where diff <= 90
      group by user_id, product_id) t2
group by product_id
order by crp desc, product_id;

46 出勤率问题

select course_id,
       cast(sum(if(total is null, 0, if(total > 2400, 1, 0))) / count(*) as decimal(16, 2)) adr
from (select t1.course_id, sum(unix_timestamp(l.login_out) - unix_timestamp(l.login_in)) total
      from (select course_id, id from course_apply lateral view explode(user_id) user_id as id) t1
               left join user_login l on t1.course_id = l.course_id and l.user_id = t1.id
      group by t1.course_id, t1.id) t2
group by course_id;

47 打车问题

select period,
       count(*)                                           get_car_num,
       cast(avg(nvl(wait, 0)) / 60 as decimal(16, 2))     wait_time,
       cast(avg(nvl(dispatch, 0)) / 60 as decimal(16, 2)) dispatch_time
from (select case
                 when hour(r.event_time) >= 7 and hour(r.event_time) < 9 then '早高峰'
                 when hour(r.event_time) >= 9 and hour(r.event_time) < 17 then '工作时间'
                 when hour(r.event_time) >= 17 and hour(r.event_time) < 20 then '晚高峰'
                 else '休息时间' end                                         period,
             unix_timestamp(o.order_time) - unix_timestamp(r.event_time) wait,
             unix_timestamp(o.start_time) - unix_timestamp(o.order_time) dispatch
      from get_car_record r
               left join get_car_order o on r.order_id = o.order_id) t1
group by period;

48 排列问题

-- 自连接
select t1.team_name team_name_1, t2.team_name team_name_2
from team t1
         join team t2 on t1.team_name > t2.team_name;

-- 开窗聚合,炸裂函数
select team_name_1, team_name_2
from (select team_name                                                                               team_name_1,
             collect_list(team_name)
                          over (order by team_name rows between 1 following and unbounded following) team_list
      from team) t2 lateral view explode(team_list) team_list as team_name_2;

49 视频热度问题

-- 结果(但是不符合题目意思)
select video_id,
       cast(ceil((whole / total + up + comment + retweet) / (datediff(today, max_dt) + 1)) as decimal(16, 1)) heat
from (select video_id,
             today,
             max(dt)                                 max_dt,
             count(*)                                total,
             sum(if(l.ts >= i.duration, 1, 0)) * 100 whole,
             sum(l.if_like) * 5                      up,
             count(l.comment_id) * 3                 comment,
             sum(l.if_retweet) * 2                   retweet
      from (select video_id,
                   unix_timestamp(end_time) - unix_timestamp(start_time) ts,
                   to_date(end_time)                                     dt,
                   to_date(max(end_time) over (partition by video_id))   today,
                   if_like,
                   comment_id,
                   if_retweet
            from user_video_log) l
               join video_info i on i.video_id = l.video_id
      where l.dt <= l.today
        and l.dt >= date_sub(l.today, 29)
      group by l.video_id, today) t1
order by heat
limit 3;

-- 题目意思
select video_id,
       cast(((whole / total + up + comment + retweet) / fresh) as decimal(16, 2)) heat
from (select video_id,
             30 - count(distinct dt) + 1             fresh,
             count(*)                                total,
             sum(if(l.ts >= i.duration, 1, 0)) * 100 whole,
             sum(l.if_like) * 5                      up,
             count(l.comment_id) * 3                 comment,
             sum(l.if_retweet) * 2                   retweet
      from (select video_id,
                   unix_timestamp(end_time) - unix_timestamp(start_time) ts,
                   to_date(end_time)                                     dt,
                   to_date(max(end_time) over ())                        today,
                   if_like,
                   comment_id,
                   if_retweet
            from user_video_log) l
               join video_info i on i.video_id = l.video_id
      where l.dt <= l.today
        and l.dt >= date_sub(l.today, 29)
      group by l.video_id) t1
order by heat
limit 3;

50 员工在职人数问题

select mth,cast(sum(num) as decimal(16,2)) ps from
(select month(dt) mth,id,sum(if((dt >= en_dt and dt <= le_dt)
    or (dt >= en_dt and le_dt is null),1,0))/count(*) num
from cal join
     emp
where dt < '2020-04-01'
  and dt >= '2020-01-01' group by month(dt),id) t2 group by mth;

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/178781.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

部署jekins遇到的问题

jdk问题 我用的jdk版本是21的结果版本太新了&#xff0c;启动jekins服务的时候总是报错最后在jekins的安装目录下面的jekinsErr.log查看日志发现是jdk问题最后换了一个17版本的就解决了。 unity和jekins jekins和Git源码管理 jekins和Git联动使用 我想让jekins每次打包的时…

OpenAI再次与Sam Altman谈判;ChatGPT Voice正式上线

11月22日&#xff0c;金融时报消息&#xff0c;OpenAI迫于超过700名员工联名信的压力&#xff0c;再次启动了与Sam Altman的谈判&#xff0c;希望他回归董事会。 在Sam确定加入微软后&#xff0c;OpenAI超700名员工签署了一封联名信&#xff0c;要求Sam和Greg Brockman&#x…

链表OJ--下

文章目录 前言一、链表分割二、环形链表I三、环形链表II四、链表的回文结构五、随机链表的复制 前言 一、链表分割 牛客网CM11&#xff1a;链表分割- - -点击此处传送 题解&#xff1a; 思路图&#xff1a; 代码&#xff1a; 二、环形链表I 力扣141&#xff1a;环形链表…

电子学会C/C++编程等级考试2022年12月(一级)真题解析

C/C++等级考试(1~8级)全部真题・点这里 第1题:加一 输入一个整数x,输出这个整数加1后的值,即x+1的值。 时间限制:1000 内存限制:65536输入 一个整数x(0 ≤ x ≤ 1000)。输出 按题目要求输出一个整数。样例输入 9样例输出 10 答案: //参考答案: #include<bits/st…

C#使用whisper.net实现语音识别(语音转文本)

目录 介绍 效果 输出信息 项目 代码 下载 介绍 github地址&#xff1a;https://github.com/sandrohanea/whisper.net Whisper.net. Speech to text made simple using Whisper Models 模型下载地址&#xff1a;https://huggingface.co/sandrohanea/whisper.net/tree…

使用 Lhotse 高效管理音频数据集

Lhotse 是一个旨在使语音和音频数据准备更具灵活性和可访问性的 Python 库&#xff0c;它与 k2 一起&#xff0c;构成了下一代 Kaldi 语音处理库的一部分。 主要目标&#xff1a; 1. 以 Python 为中心的设计吸引更广泛的社区参与语音处理任务。 2. 为有经验的 Kaldi 用户提供…

13.求面积[有问题]

#include<stdio.h> #include<math.h> #include<bits/stdc.h> using namespace std;void fun(double a,b,c) {double p,c;p (abc)/2;c sqrt(p*(p-a)*(p-b)*(p-c));printf("面积是&#xff1a;%lf",c); }int main(){double a,b,c;scanf("%lf,%…

【机器学习】Nonlinear Independent Component Analysis - Aapo Hyvärinen

Linear independent component analysis (ICA) x i ( k ) ∑ j 1 n a i j s j ( k ) for all i 1 … n , k 1 … K ( ) x_i(k) \sum_{j1}^{n} a_{ij}s_j(k) \quad \text{for all } i 1 \ldots n, k 1 \ldots K \tag{} xi​(k)j1∑n​aij​sj​(k)for all i1…n,k1…K()…

DedeBIZ 管理系统 DedeV6 v6.2.6 社区版 免费授权版

DedeBIZ 系统&#xff1a;开源、安全、高效的 DedeV6 v6.2.6 社区版 DedeBIZ 系统是基于 PHP 7 版本开发的&#xff0c;具有强大的可扩展性&#xff0c;并且完全开放源代码。它采用现流行的 Go 语言设计开发&#xff0c;不仅拥有简单易用、灵活扩展的特性&#xff0c;还具备更…

2019年12月 Scratch(三级)真题解析#中国电子学会#全国青少年软件编程等级考试

Scratch等级考试(1~4级)全部真题・点这里 一、单选题(共25题,每题2分,共50分) 第1题 怎样修改图章的颜色? A:只需要一个数字来设置颜色 B:设置RGB的值 C:在画笔中设置颜色、饱和度、亮度 D:在外观中设置或修改角色颜色特效 答案:D 在外观中设置或修改角色颜色特…

基于北方苍鹰算法优化概率神经网络PNN的分类预测 - 附代码

基于北方苍鹰算法优化概率神经网络PNN的分类预测 - 附代码 文章目录 基于北方苍鹰算法优化概率神经网络PNN的分类预测 - 附代码1.PNN网络概述2.变压器故障诊街系统相关背景2.1 模型建立 3.基于北方苍鹰优化的PNN网络5.测试结果6.参考文献7.Matlab代码 摘要&#xff1a;针对PNN神…

结构体打印

打印输出 通过注解来派生Debug trait&#xff0c;才可以通过println!进行打印。默认的占位符是{}&#xff0c;底层是按照std::fmt::Display具体实现进行格式化输出。 {}、{:?}、{#?}是格式化的几种形式&#xff0c;{#?}是更加易读的JSON话格式。 方法 结构体声明方法&…

深兰科技多款大模型技术产品登上新闻联播!

11月20日晚&#xff0c;新闻联播报道了2023中国5G工业互联网大会&#xff0c;深兰科技metamind、汉境大型城市智能体空间等大模型技术和产品在众多参展产品中脱颖而出&#xff0c;被重点播报。 2023中国5G工业互联网大会 本届大会由工信部和湖北省人民政府联合主办&#xff0c;…

阿里云服务器ECS经济型e实例优惠99元性能怎么样?

阿里云服务器ECS经济型e实例优惠99元性能怎么样&#xff1f;阿里云服务器优惠99元一年&#xff0c;配置为云服务器ECS经济型e实例&#xff0c;2核2G配置、3M固定带宽和40G ESSD Entry系统盘&#xff0c;CPU采用Intel Xeon Platinum架构处理器&#xff0c;2.5 GHz主频&#xff0…

第三节-Android10.0 Binder通信原理(三)-ServiceManager篇

1、概述 在Android中&#xff0c;系统提供的服务被包装成一个个系统级service&#xff0c;这些service往往会在设备启动之时添加进Android系统&#xff0c;当某个应用想要调用系统某个服务的功能时&#xff0c;往往是向系统发出请求&#xff0c;调用该服务的外部接口。在上一节…

迁新址 启新程|美创科技杭州总部乔迁仪式圆满举行

“迁新址 启新程” 2023年11月21日 美创科技杭州总部乔迁仪式隆重举行 杭州未来科技城管委会、余杭国投集团、浙江省网络空间安全协会、浙江鸿程、华睿投资、金艮投资、如山资本、赛伯乐投资、宽带资本、普华投资、国中创投、密码资本、东方富海、之江商学、阿里云、联通&…

系列八、key是弱引用,gc垃圾回收时会影响ThreadLocal正常工作吗

一、key是弱引用&#xff0c;gc垃圾回收时会影响ThreadLocal正常工作吗 到这里&#xff0c;有些小伙伴可能有疑问&#xff0c;ThreadLocalMap的key既然是 弱引用&#xff0c;那么GC时会不会贸然地把key回收掉&#xff0c;进而影响ThreadLocal的正常使用呢&#xff1f;答案是不会…

【LeetCode:2304. 网格中的最小路径代价 | dijkstra(迪杰斯特拉)】

&#x1f680; 算法题 &#x1f680; &#x1f332; 算法刷题专栏 | 面试必备算法 | 面试高频算法 &#x1f340; &#x1f332; 越难的东西,越要努力坚持&#xff0c;因为它具有很高的价值&#xff0c;算法就是这样✨ &#x1f332; 作者简介&#xff1a;硕风和炜&#xff0c;…

6-使用nacos作为注册中心

本文讲解项目中集成nacos&#xff0c;并将nacos作为注册中心使用的过程。本文不涉及nacos的原理。 1、项目简介 以一个演示项目为例&#xff0c;项目包含三个服务&#xff0c;调用及依赖如下图&#xff1a; 由图中可以看出&#xff0c;coupon-customer-serv为服务的消费者&a…

SpringMVC(五)SpringMVC的视图

SpringMVC中的视图是View接口&#xff0c;视图的作用渲染数据&#xff0c;将模型Model中的数据展示给用户 SpringMVC视图的种类很多&#xff0c;默认有转发视图(InternalResourceView)和重定向视图(RedirectView) 当工程引入jstl的依赖&#xff0c;转发视图会自动转换为JstlV…