Skip to content

Instantly share code, notes, and snippets.

View RedShift1's full-sized avatar

Glenn Matthys RedShift1

View GitHub Profile
@RedShift1
RedShift1 / postgres_pivotcursor.sql
Created July 6, 2020 09:57 — forked from flashpixx/postgres_pivotcursor.sql
postgres - dynamic pivot-table cursor with column array aggregation
--- description: creates a dynamic temporary pivot table:
--- first argument is the name of the pivot-table
--- second the data select
--- third an array of key fields
--- fourth an array of pivot columns
--- fifth an array of returning fields
---
--- usage (example): select * from pivottable( 'mypivot', 'select * from mydata', array['id', 'name', 'description'], array['parametername'], array['parametervalue::text', 'parametervaluetext'] ); fetch all from mypivot;
CREATE OR REPLACE FUNCTION pivottable(
const typeDefs = `
type Comment {
id: Int!,
body: String!,
postId: Int,
authorId: Int,
archived: Boolean
}
type Post {