transpose column headers to rows in postgresql

前端 未结 5 1510
悲哀的现实
悲哀的现实 2020-12-06 00:59

I have a view which looks like this

          value1count     value2count value3count
          ----------------------------------------
             25             


        
5条回答
  •  误落风尘
    2020-12-06 01:17

    Crosstab only does the reverse of what you need, but this should help you:

    First create the unnest() function that is included in 8.4, see here for instructions.

    Then you can do this (based on this post):

    SELECT
       unnest(array['value1Count', 'value2Count', 'value3Count']) AS "Values",
       unnest(array[value1Count, value2Count, value3Count]) AS "Count"
    FROM view_name
    ORDER BY "Values"
    

    I can verify that this works in 8.4, but because I don't have 8.1, I can't promise it will work the same.

提交回复
热议问题