Create dynamic table from function in PostgreSQL

后端 未结 3 1661
死守一世寂寞
死守一世寂寞 2021-01-06 12:14

I have table data,

select * from tbltaxamount ;
 id |   taxname   | taxinfoid | taxvalue | taxamt | zoneid | invoiceid | transid 
----+-------------+--------         


        
3条回答
  •  迷失自我
    2021-01-06 13:09

    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).

提交回复
热议问题