Poor man's pivot with CASE
I would aggregate in a subquery to simplify. Probably faster, too:
SELECT score
, CASE WHEN name = 'A' AND nick = 'X' THEN sum_time ELSE 0 END AS a_x
, CASE WHEN name = 'B' AND nick = 'Z' THEN sum_time ELSE 0 END AS b_z
FROM (
SELECT score, name, nick, COALESCE(sum(time), 0) AS sum_time
FROM table1
GROUP BY 1,2,3
) sub
ORDER BY score, name, nick;
crosstab()
Read basic instructions here, first:
Since crosstab()
expects a single column for the key value, generate a unique key from the combination of (name, nick)
:
SELECT * FROM crosstab(
$$
SELECT score
, CASE WHEN name = 'A' AND nick = 'X' THEN 1
WHEN name = 'B' AND nick = 'Z' THEN 2 END AS key
, COALESCE(sum(time), 0) AS val
FROM table1
WHERE (name = 'A' AND nick = 'X' OR
name = 'B' AND nick = 'Z') -- exclude rest (optional, faster)
GROUP BY name, nick, score
ORDER BY key, score; -- (!) reversed order to get your untypical result
$$
,'VALUES (1),(2)' -- match key values in order
) AS t (score int, a_x int, b_z int);
Produces your result (except for your error in the last line).
Typically, however, you would want:
ORDER BY score, key; -- (!) typical case
To get this result (all values for same score in one row):
score | A_time_X | B_time_Z
1 6 6
4 2 7
10 7 NULL
SQL Fiddle without crosstab()
because it's currently not possible to install additional modules there.
Dynamic query?
Hard to come by, as @Andomar already comemnted. See what's possible here: