PostgreSQL 9.3: Dynamic pivot table
You can do this with crosstab() from the additional module tablefunc: SELECT b , COALESCE(a1, 0) AS “A1” , COALESCE(a2, 0) AS “A2” , COALESCE(a3, 0) AS “A3” , … — all the way up to “A30” FROM crosstab( ‘SELECT colb, cola, 1 AS val FROM matrix ORDER BY 1,2’ , $$SELECT ‘A’::text || g … Read more