I have this database called e-classroom with tables :
tbl_enrollment
tbl_feenames
tbl_feetypes
tbl_payments
tbl_pupil
this is the view that I made out from those tables :
view_paylist
and I have this MySQL query that will pivot these tables :
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT('MAX(IF(vp.cfeename = ''',vp.cfeename,
''', vp.nfeetypeamount, NULL)) AS ',
CONCAT("`",vp.cfeename,"`")
)
) INTO @sql
FROM view_paylist AS vp;
SET @sql = CONCAT('SELECT vp.clrn
, vp.name
, vp.cgrdlvl
, csection
, vp.csy, ', @sql, '
FROM view_paylist vp
where vp.cgrdlvl like ''%''
group by vp.clrn
');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
and this is the result of the pivot :
the field "sample" is my newly added data to tbl_feenames. What I want is how can I achieve this dynamic pivoting in PostgreSQL using Crosstab ??