I have a scores table like this:
code | week | points
1001 | 1 | 2
1001 | 1 | 1
1001 | 3 | 6
2001 | 1 | 0
2001 | 4 | 5
2001 | 4 | 2
What I'd like is a result like this:
code | 1 | 3 | 4
1001 | 3 | 6 |
2001 | 0 | | 7
I've written a simple group by which I could probably use to write some code around but I'd rather do the work in the SQL. http://sqlfiddle.com/#!15/8ff5d
select code, week, sum(points) as total
from scores
group by code, week
order by code, week;
And the result is:
code | week | total
1001 | 1 | 3
1001 | 3 | 6
2001 | 1 | 0
2001 | 4 | 7
I'm sure it's really simple but I'm stumped. Thanks in advance for any help.