I have table data,
select * from tbltaxamount ;
id | taxname | taxinfoid | taxvalue | taxamt | zoneid | invoiceid | transid
----+-------------+--------
Could look like this:
SELECT invoiceid
,sum(CASE WHEN taxname = 'Service Tax' THEN taxamt ELSE 0 END) AS "Service Tax"
,sum(CASE WHEN taxname = 'ABC Tax' THEN taxamt ELSE 0 END) AS "ABC Tax"
FROM tbltaxamount
GROUP BY 1
Depending on what you actually want to achieve, you might be interested in the tablefunc module that can be used to create pivot tables. Here is an example.
If you insist on column names derived from data, you will have build your query dynamically, with a plpgsql function like you did or an anonymous code block (DO
statement).