V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
helloworld000
V2EX  ›  问与答

sql 写法求解

  •  
  •   helloworld000 · 2020-03-23 07:06:47 +08:00 · 1096 次点击
    这是一个创建于 1500 天前的主题,其中的信息可能已经有所发展或是发生改变。

    看到一个面试题想请假一下

    two tables:

    friending (sender_id, receiver_id, send_date, accept_date, sender_country)

    age (user_id, age_group)

    1.1 How many friend-requests sent on each day of the week for the last 4 weeks?

    1.2 What is the average number of friend-requests sent per user over the last past week by age group? // 前 7 天内每个 age group 的平均好友请求数

    第 1 个是直接 select sender_id 然后 where send_data < 28 吧?

    主要是想请问一下第 2 个怎么写的? 这 2 个 tables union,然后 COUNT(sender_id) OVER (PARTITION BY age_group) 这种?

    不好意思 sql 不太懂临时抱佛脚学的比较乱七八糟,希望指教

    2 条回复    2020-03-23 09:31:05 +08:00
    hand515
        1
    hand515  
       2020-03-23 08:46:04 +08:00
    Join
    a87965028
        2
    a87965028  
       2020-03-23 09:31:05 +08:00   ❤️ 1
    select a.age_group, count(1) / count(distinct a.user_id) as avg
    from friending as f
    inner join age as a on f.sender_id = a.user_id
    where send_date < datediff(day, -7, getdate())
    group by age_group
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2268 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 09:37 · PVG 17:37 · LAX 02:37 · JFK 05:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.