前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >面试篇:两道常见面试sql题

面试篇:两道常见面试sql题

作者头像
伊泽瑞尔
发布2022-06-01 08:44:11
2110
发布2022-06-01 08:44:11
举报

1.有一个订单表order_tab,字段有:

order_id,order_amt,user_id,user_address

计算每个用户使用最多的3个地址,以及每个地址使用的次数,对应地址消费的总金额;

代码语言:javascript
复制
select 
  user_id, 
  user_address, 
  cnt, 
  order_amt 
from 
  (
    select 
      user_id, 
      user_address, 
      cnt, 
      row_number() over(partition by user_id order by cnt desc) rn, 
      order_amt 
    from 
      (
        select 
          user_id, 
          user_address, 
          count(1) as cnt, 
          sum(order_amt) as order_amt 
        from 
          order_tab 
        group by 
          user_id, 
          user_address
      ) t
  ) t1 
where rn <= 3

2.最近半年连续7天访问数

代码语言:javascript
复制
select 
  count(distinct user_id) 
from 
  (
    select 
      user_id, 
      row_number() over(partition by user_id order by continue_day) rn1 
    from 
      (
        select 
          user_id, 
          date_sub(dt, rn) continue_day 
        from 
          (
            select 
              user_id, 
              dt, 
              row_number() over(partition by user_id order by dt) rn 
            from log where dt between '20211102' and '20220502'
          ) t
      ) t1 
    where rn1 >= 7
  ) t2
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-05-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 大数据与知识图谱 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档