如何创建视图并比较同一列中的值?

时间:2018-08-30 21:31:29

标签: sqlite

我有两个表,表“项目”和表“学生”,每个都有三列。我想创建一个称为团队合作的视图,该视图列出了具有团队合作的学生对。视图中的每一行应该是一对至少有2个项目一起参加同一项目的2名学生,并且这些项目的平均分数> = 35。视图应采用以下格式:

teamwork (student_id1, student_id2, project_count, average_project_score).

仅保留其student_id1

table "projects" and table "students"

我的下面代码实际上计算了项目的总体平均得分,以及每个学生参加了多少项目,而不是一对学生合作了多少项目。谁能帮忙解决这个问题?

create teamwork as 
select
student_id, count(student_id) as project_count, avg(score) as 
average_project_score from students
inner join projects on project_id=students.project_id 
where projects.score >= 35
group by students.student_id
having count(students.student_id)>=2;

select A.student_id, B.student_id, A.project_count, A.average_project_score
from teamwork as A, teamwork as B
where A.student_id < B.student_id
order by A.cast_id,B.cast_id;

0 个答案:

没有答案