sql inner join加上sum

时间:2018-01-19 17:27:08

标签: sql

有没有办法显示如下所示的输出和每人的总安装量分配。 在这种情况下,Bob White共有2个作业,其余为1。

我尝试通过assignments.codeassignment进行分组,但这不起作用。

SELECT students.namestudent, assignments.homework, 
assignments.codeassignment, courses.codecourse, courses.namecourse
FROM students
INNER JOIN assignments
ON students.idstudent = assignments.idstudent
INNER JOIN courses
on assignments.codecourse = courses.codecourse;

output:
'Jack Brown', 'finnish assignment a', 1111, 123, 'math'
'Alice Black', 'finnish assignment b', 4444, 256, 'algebra'
'Bob White', 'finnish assignment c', 2222, 555, 'game project'
'Dan Purple', 'finnish assignment d', 3333,548, 'analysis'
'Bob White', 'finnish assignment c', 4444, 256, 'algebra'

1 个答案:

答案 0 :(得分:1)

您可以使用窗口功能:

Atomics

SELECT s.namestudent, a.homework, a.codeassignment, c.codecourse, c.namecourse, COUNT(*) OVER (PARTITION BY s.namestudent) FROM students s INNER JOIN assignments a ON s.idstudent = a.idstudent INNER JOIN courses c ONn a.codecourse = c.codecourse; 是大多数数据库中可用的ANSI标准功能。