sql 写法求解

2020-03-23 07:06:47 +08:00
 helloworld000

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

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 不太懂临时抱佛脚学的比较乱七八糟,希望指教

1123 次点击
所在节点    问与答
2 条回复
hand515
2020-03-23 08:46:04 +08:00
Join
a87965028
2020-03-23 09:31:05 +08:00
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

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/655217

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX