简体   繁体   中英

sql inner join plus sum

Is there a way to show the output as seen below AND the total ammount assignments per person. In this case Bob White has a total of 2 assignments, the rest 1.

I tried group by assignments.codeassignment but that did not work.

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'

You can use window functions:

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;

COUNT(*) OVER (. . .) is ANSI-standard functionality available in most databases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM