sql - Grouping by as a single row with results as a column? -
this question has answer here:
- create pivot table postgresql 1 answer
i have scores table 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 result this:
code | 1 | 3 | 4 1001 | 3 | 6 | 2001 | 0 | | 7
i've written simple group use write code around i'd rather work in sql. http://sqlfiddle.com/#!15/8ff5d
select code, week, sum(points) total scores group code, week order code, week;
and result is:
code | week | total 1001 | 1 | 3 1001 | 3 | 6 2001 | 1 | 0 2001 | 4 | 7
i'm sure it's simple i'm stumped. in advance help.
you're looking pivot function:
similar question: create pivot table postgresql
when number of columns fix, it's more or less simple. if number dynamic, search dynamic pivot.
Comments
Post a Comment