bZhang bZhang - 7 months ago 8
SQL Question

How to group two fields together in SQL?

say I have a sql that currently returns all soccer players who has played during each years. Like so:

name year goals
john 2010 1
john 2006 2
john 2006 8
fred 2006 1


But I want the result to be grouped by the years they played, but do not compress player names if they are from different years, like so:

name year goals
john 2010 1
john 2006 10 <--- This is compressed, but there are still 2 johns
fred 2006 1 since they are from different years


say I have done this so far.

(select name, year, goals
from table) as T


If I just do

select *
from
(select name, year, goals
from table) as T
group by year;


Fred will disappear, but if I do "group by name", there are only 1 john left. Any help?

Answer
select name, year, sum(goals) as totalgoals
from table
group by name, year