sql group by with inner join

时间:2018-01-19 15:49:45

标签: sql

我想根据此查询的结果进行分组,如下所示。

select takes.namestudent, takes.pointscourse    
from takes    
inner join follows    
on follows.idstudent = takes.idstudent    
where follows.completedprogram = 'yes'

结果:

'Dan Purple',4
'Dan Purple',2
'Dan Purple',3
'Dan Purple',2
'Dan Purple',2
'Bob White',2
'Bob White',4
'Bob White',3
'Bob White',3
'Bob White',2

但我希望这样:

'Dan Purple', 13
'Bob White', 14

我尝试了几个小组或总结可能性,但我无法做到正确。 我做错了什么?

1 个答案:

答案 0 :(得分:2)

select takes.namestudent, sum(takes.pointscourse) as total
from takes 
inner join follows on follows.idstudent = takes.idstudent 
where follows.completedprogram = 'yes'
group by takes.namestudent