当前位置:网站首页>【每日SQL打卡】DAY 21丨每个帖子的评论数【难度中等】
【每日SQL打卡】DAY 21丨每个帖子的评论数【难度中等】
2022-07-29 11:43:00 【不吃西红柿】
难度简单
SQL架构
表 Submissions 结构如下:
+---------------+----------+
| 列名 | 类型 |
+---------------+----------+
| sub_id | int |
| parent_id | int |
+---------------+----------+
上表没有主键, 所以可能会出现重复的行。
每行可以是一个帖子或对该帖子的评论。
如果是帖子的话,parent_id 就是 null。
对于评论来说,parent_id 就是表中对应帖子的 sub_id。编写 SQL 语句以查找每个帖子的评论数。
结果表应包含帖子的 post_id 和对应的评论数 number_of_comments 并且按 post_id 升序排列。
Submissions 可能包含重复的评论。您应该计算每个帖子的唯一评论数。
Submissions 可能包含重复的帖子。您应该将它们视为一个帖子。
查询结果格式如下例所示:
Submissions table:
+---------+------------+
| sub_id | parent_id |
+---------+------------+
| 1 | Null |
| 2 | Null |
| 1 | Null |
| 12 | Null |
| 3 | 1 |
| 5 | 2 |
| 3 | 1 |
| 4 | 1 |
| 9 | 1 |
| 10 | 2 |
| 6 | 7 |
+---------+------------+
结果表:
+---------+--------------------+
| post_id | number_of_comments |
+---------+--------------------+
| 1 | 3 |
| 2 | 2 |
| 12 | 0 |
+---------+--------------------+
表中 ID 为 1 的帖子有 ID 为 3、4 和 9 的三个评论。表中 ID 为 3 的评论重复出现了,所以我们只对它进行了一次计数。
表中 ID 为 2 的帖子有 ID 为 5 和 10 的两个评论。
ID 为 12 的帖子在表中没有评论。
表中 ID 为 6 的评论是对 ID 为 7 的已删除帖子的评论,因此我们将其忽略。边栏推荐
- Building and sharing the root of the digital world: Alibaba Cloud builds a comprehensive cloud-native open source ecosystem
- MarkDown高阶语法手册
- HMS Core Discovery第16期回顾|与虎墩一起,玩转AI新“声”态
- [image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code
- PL/SQL 集合
- 递归-八皇后问题
- 2022年企业直播行业发展洞察
- LMO·3rd - 报名通知
- Starrocks technology insider: how to have both real-time update and fast query
- 【年中总结】创业3年,越来越穷,还是坚持架构平台
猜你喜欢

Codeforces Round #797 (Div. 3)个人题解

Why should kubernetes be used in development environments

微信云托管入门与实践

Starrocks technology insider: how to have both real-time update and fast query

How to start writing helm charts for your kubernetes application

宝塔快速搭建自适应咖啡网站模板与管理系统源码实测

The interviewer training courseware (very practical in-house training courseware)

Kubernetes基本概念

ECCV 2022 | SSP: 自支持匹配的小样本任务新思想

如何使用“COPY –link”加速 Docker 构建和优化缓存
随机推荐
How to start writing helm charts for your kubernetes application
INVALID_ ARGUMENT : Invalid rank for input: modelInput Got: 3 Expected: 4 Please fix either the input
XSS漏洞分析
2.2选择排序
公司出了一款新产品,要不要招代理商?
LMO·3rd - 报名通知
解决 Chrome 浏览器被毒霸篡改问题
std::vector 拷贝、追加、嵌套访问
ASN.1接口描述语言详解
小笑授权系统V5.0开心版
Great golang Road
Peking University open classes are coming! Welcome to the "AI for science" class
【表达式计算】表达式计算问题的通用解法(练习加强版,含总结)
如何开始为您的 Kubernetes 应用程序编写 Helm 图表
593. 有效的正方形
企业微信客户朋友圈一天可以发多少条?都有哪些限制?如何突破朋友圈可展示人数限制?
Exclusive interview | Cheng Li, chief technology officer of Alibaba: cloud + open source together form a credible foundation for the digital world
LED透明屏和LED玻璃显示屏区别
QML(一):自定义圆角按钮的处理
基础架构之分布式配置中心