HQL刷题 50道

news2025/1/12 19:59:22

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;

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

分类预测 | Matlab实现基于DBN-SVM深度置信网络-支持向量机的数据分类预测

分类预测 | Matlab实现基于DBN-SVM深度置信网络-支持向量机的数据分类预测 目录 分类预测 | Matlab实现基于DBN-SVM深度置信网络-支持向量机的数据分类预测分类效果基本描述程序设计参考资料 分类效果 基本描述 1.利用DBN进行特征提取&#xff0c;将提取后的特征放入SVM进行分类…

【Delphi】开发IOS 程序,TLabel 中英文字对齐(水平),一行代码解决显示对齐问题!

目录 一、问题现象&#xff1a; 二、解决方案&#xff08;一行代码解决ios对齐问题&#xff09;&#xff1a; 三、解决后效果&#xff1a; 四、后记&#xff1a; 一、问题现象&#xff1a; 在用 Delphi 开发ios程序时&#xff0c;使用TLabel控件显示&#xff0c;会出现中英…

《工程测量学》笔记/期末复习资料

水平角观测方法&#xff1a; ①测回法&#xff1b;②方向观测法&#xff08;全圆观测法&#xff09;。 比例尺精度&#xff1a; 图上0.1mm&#xff08;肉眼能够识别的最小距离&#xff09;所表示的实地距离称为“比例尺精度”。 ①尺寸小于比例尺精度的地物不需要测量&…

部署jekins遇到的问题

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

SpringMVC(五)SpringMVC的视图

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

鸿蒙4.0开发笔记之ArkTs语言基础与基本组件结构(四)

文章声明&#xff1a;本文关于HarmonyOS系统的部分内容和描述借鉴于华为官网的“HarmonyOS开发者学堂”&#xff0c;有需要的也可以进入官网查看。<HarmonyOS第一课>ArkTS开发语言介绍 一、ArkTs语言介绍 ArkTS是鸿蒙系统&#xff08;HarmonyOS&#xff09;优选的主力应…

竞赛 : 题目:基于深度学习的水果识别 设计 开题 技术

1 前言 Hi&#xff0c;大家好&#xff0c;这里是丹成学长&#xff0c;今天做一个 基于深度学习的水果识别demo 这是一个较为新颖的竞赛课题方向&#xff0c;学长非常推荐&#xff01; &#x1f9ff; 更多资料, 项目分享&#xff1a; https://gitee.com/dancheng-senior/pos…

【机器学习】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()…

如何访问linux上的web服务

1.获取服务运行端口 例如8080 2.如果时vmware 需要先配置转发端口和主机ip 主机ip需要未使用的 例如&#xff1a; 3.查看虚拟机防火墙设置 centos8 为例 &#xff1a; firewall-cmd --zonepublic --list-ports 查看放通端口 如果没有放通 firewall-cmd --zonepublic --add-p…

使用Pytorch从零开始构建CGAN (conditional GAN)

GAN和DCGAN生成随机图像。因此&#xff0c;我们几乎无法控制生成哪些图像。然而&#xff0c;CGAN 可以让我们指定一个条件&#xff0c;以便我们可以告诉它要生成哪些图像。诀窍是使用可学习层将标签值转换为特征向量&#xff0c;以便生成器可以学习要生成什么图像。鉴别器还利用…

时序预测 | MATLAB实现基于BiLSTM-AdaBoost双向长短期记忆网络结合AdaBoost时间序列预测

时序预测 | MATLAB实现基于BiLSTM-AdaBoost双向长短期记忆网络结合AdaBoost时间序列预测 目录 时序预测 | MATLAB实现基于BiLSTM-AdaBoost双向长短期记忆网络结合AdaBoost时间序列预测预测效果基本介绍模型描述程序设计参考资料 预测效果 基本介绍 1.Matlab实现BiLSTM-Adaboost…

【OpenCV实现图像:使用OpenCV进行图像处理之透视变换】

文章目录 概要计算公式举个栗子实际应用小结 概要 透视变换&#xff08;Perspective Transformation&#xff09;是一种图像处理中常用的变换手段&#xff0c;它用于将图像从一个视角映射到另一个视角&#xff0c;常被称为投影映射。透视变换可以用于矫正图像中的透视畸变&…

ChatGPT规模化服务的经验与教训

2022年11月30日&#xff0c;OpenAI发布ChatGPT&#xff0c;以很多人未曾预料的速度迅速走红。与此同时&#xff0c;由于短时间内用户量的暴涨&#xff0c;导致服务器过载&#xff0c;迫使OpenAI停止新用户的注册。 ChatGPT发布这一年&#xff0c;同样的情景发生了好几次。在最近…

【追求卓越12】算法--堆排序

引导 前面几节&#xff0c;我们介绍了有关树的数据结构&#xff0c;我们继续来介绍一种树结构——堆。堆的应用场景有很多&#xff0c;比如从大量数据中找出top n的数据&#xff1b;根据优先级处理网络请求&#xff1b;这些情景都可以使用堆数据结构来实现。 什么是堆&#xf…

[论文笔记] Scaling Laws for Neural Language Models

概览: 一、总结 计算量、数据集大小、模型参数量大小的幂律 与 训练损失呈现 线性关系。 三个参数同时放大时,如何得到最佳的性能? 更大的模型 需要 更少的样本 就能达到相同的效果。 </

Doris数据模型的选择建议(十三)

Doris 的数据模型主要分为 3 类&#xff1a;Aggregate、Uniq、Duplicate Aggregate: Doris 数据模型-Aggregate 模型 Uniq&#xff1a;Doris 数据模型-Uniq 模型 Duplicate&#xff1a;Doris 数据模型-Duplicate 模型 因为数据模型在建表时就已经确定&#xff0c;且无法修改…

鸿蒙4.0开发笔记之DevEco Studio页面操作router的pushUrl页面跳转与back返回上一页(五)

一、认识组件 关于HarmonyOS中ArkTS的基础组件请参见文章鸿蒙4.0开发笔记之ArkTs语言基础与基本组件结构&#xff08;四&#xff09; 二、实现页面跳转pushUrl 1、操作说明 实现页面跳转的核心便是router.pushUrl的调用&#xff0c;操作起来也很简单&#xff0c;总共就四步…

CSDN等级权益概览

文章目录 一、[权益概览](https://blog.csdn.net/SoftwareTeacher/article/details/114499372)二、权益详情&#xff08;更新中...&#xff09;2.1、等级权益2.2、原创保护2.3、推广管理2.4、博客皮肤 一、权益概览 级别对应分数解释权益未定级0这类用户没有做任何贡献。或者曾…

【done+重点】剑指Offer56-I:找出数组中2个只出现1次的整数

力扣&#xff0c;https://leetcode.cn/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof/description/ 题目&#xff1a;一个整型数组nums里除两个数字之外&#xff0c;其他数字都出现了两次。请写程序找出这两个只出现一次的数字。要求时间复杂度是O(n)&#xff0c;空间…

二叉搜索树java实现

顾名思义&#xff0c;二叉搜索树是一棵二叉树&#xff0c;每个节点就是一个对象&#xff0c;这个对象包含属性left、right和parent。left指向节点的左孩子&#xff0c;right指向节点的右孩子&#xff0c;parent指向节点的父节点&#xff08;双亲&#xff09;。如果某个孩子节点…