当前位置: 首页> 教育> 培训 > 游戏工作室加盟_百度关键词优化快速排名软件_如何做运营推广_电商平台排名

游戏工作室加盟_百度关键词优化快速排名软件_如何做运营推广_电商平台排名

时间:2025/7/13 8:04:10来源:https://blog.csdn.net/TuringSnowy/article/details/142523988 浏览次数:0次
游戏工作室加盟_百度关键词优化快速排名软件_如何做运营推广_电商平台排名
  1. Students and Examinations

Problem description

Table: Students

±--------------±--------+
| Column Name | Type |
±--------------±--------+
| student_id | int |
| student_name | varchar |
±--------------±--------+
student_id is the primary key (column with unique values) for this table.
Each row of this table contains the ID and the name of one student in the school.

Table: Subjects

±-------------±--------+
| Column Name | Type |
±-------------±--------+
| subject_name | varchar |
±-------------±--------+
subject_name is the primary key (column with unique values) for this table.
Each row of this table contains the name of one subject in the school.

Table: Examinations

±-------------±--------+
| Column Name | Type |
±-------------±--------+
| student_id | int |
| subject_name | varchar |
±-------------±--------+
There is no primary key (column with unique values) for this table. It may contain duplicates.
Each student from the Students table takes every course from the Subjects table.
Each row of this table indicates that a student with ID student_id attended the exam of subject_name.

Write a solution to find the number of times each student attended each exam.

Return the result table ordered by student_id and subject_name.

The result format is in the following example.

Example 1:

Input:
Students table:
±-----------±-------------+
| student_id | student_name |
±-----------±-------------+
| 1 | Alice |
| 2 | Bob |
| 13 | John |
| 6 | Alex |
±-----------±-------------+
Subjects table:
±-------------+
| subject_name |
±-------------+
| Math |
| Physics |
| Programming |
±-------------+
Examinations table:
±-----------±-------------+
| student_id | subject_name |
±-----------±-------------+
| 1 | Math |
| 1 | Physics |
| 1 | Programming |
| 2 | Programming |
| 1 | Physics |
| 1 | Math |
| 13 | Math |
| 13 | Programming |
| 13 | Physics |
| 2 | Math |
| 1 | Math |
±-----------±-------------+
Output:

student_idstudent_namesubject_nameattended_exams
1AliceMath3
1AlicePhysics2
1AliceProgramming1
2BobMath1
2BobPhysics0
2BobProgramming1
6AlexMath0
6AlexPhysics0
6AlexProgramming0
13JohnMath1
13JohnPhysics1
13JohnProgramming1

Explanation:
The result table should contain all students and all subjects.
Alice attended the Math exam 3 times, the Physics exam 2 times, and the Programming exam 1 time.
Bob attended the Math exam 1 time, the Programming exam 1 time, and did not attend the Physics exam.
Alex did not attend any exams.
John attended the Math exam 1 time, the Physics exam 1 time, and the Programming exam 1 time.

My solution

The first WITH:

SELECT * FROM Students sCROSS JOIN Subjects su

用 CROSS JOIN 获取所有学生和科目的组合,不用有相同的列来 JOIN ON.
Output:

student_idstudent_namesubject_name
1AliceProgramming
1AlicePhysics
1AliceMath
2BobProgramming
2BobPhysics
2BobMath
13JohnProgramming
13JohnPhysics
13JohnMath
6AlexProgramming
6AlexPhysics
6AlexMath

The second WITH:

    SELECT student_id, subject_name, COUNT(*) AS attended_examsFROM ExaminationsGROUP BY student_id, subject_name

Output:

student_idsubject_nameattended_exams
1Math3
1Physics2
1Programming1
2Programming1
13Math1
13Programming1
13Physics1
2Math1

Combine them:

WITH a AS (SELECT * FROM Students sCROSS JOIN Subjects su
), 
j AS (SELECT student_id, subject_name, COUNT(*) AS attended_examsFROM ExaminationsGROUP BY student_id, subject_name
)
SELECT a.student_id, a.student_name, a.subject_name, COALESCE(j.attended_exams, 0) AS attended_exams
FROM a
LEFT JOIN jON a.student_id = j.student_idAND a.subject_name = j.subject_name
ORDER BY a.student_id ASC, a.subject_name ASC

注意一定要选择 a.subject_name!因为只有这个表是全的。若选择错了,有人的 subject_name 会是 null.

关键字:游戏工作室加盟_百度关键词优化快速排名软件_如何做运营推广_电商平台排名

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: