Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

transpose column headers to rows in postgresql

Tags:

sql

postgresql

I have a view which looks like this

          value1count     value2count value3count
          ----------------------------------------
             25              35          55

I need to transpose the column header into rows and so I need it to look like

          Values              Count
         -----------------------------
           value1count         25
           value2count         35
           value3count         55

I can do this by selecting individual column names as first column and data as second column and then do a union of the same for all columns.

Is there a better way to do this? I am using PosgreSQL 8.1 and so don't have pivot operators to work with.

Thanks for your response in advance.

like image 712
cableload Avatar asked May 16 '12 18:05

cableload


People also ask

How do I switch columns in PostgreSQL?

Postgres currently defines column order based on the attnum column of the pg_attribute table. The only way to change column order is either by recreating the table, or by adding columns and rotating data until you reach the desired layout.

What is crosstab in PostgreSQL?

The crosstab function produces one output row for each consecutive group of input rows with the same row_name value. The output row_name column, plus any “extra” columns, are copied from the first row of the group. The output value columns are filled with the value fields from rows having matching category values.

How do I change the view in PostgreSQL?

ALTER VIEW [ IF EXISTS ] name ALTER [ COLUMN ] column_name SET DEFAULT expression ALTER VIEW [ IF EXISTS ] name ALTER [ COLUMN ] column_name DROP DEFAULT ALTER VIEW [ IF EXISTS ] name OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER } ALTER VIEW [ IF EXISTS ] name RENAME [ COLUMN ] column_name TO ...


2 Answers

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.

like image 105
PinnyM Avatar answered Oct 14 '22 18:10

PinnyM


I achieved your goal by using hstore's functionalities :

SELECT (x).key, (x).value
FROM
  ( SELECT EACH(hstore(t)) as x
    FROM   t
  ) q;

May you have multiple rows in your "to-be-exploded" view or table (referred here as t), you might need to insert an additionnal identifier in the intermediate table q, for example:

SELECT id, (x).key, (x).value
FROM
  ( SELECT id, EACH(hstore(t)) as x
    FROM   t
  ) q;

Reference: hstore documentation

like image 45
Mickaël Le Baillif Avatar answered Oct 14 '22 18:10

Mickaël Le Baillif