How to pivot a single row summary in postgresql

410 views Asked by At

SQLFiddle here: http://sqlfiddle.com/#!15/e8e49/2/0

I want to get some summary data from my table into rows instead of columns..

select 
  sum(n),
  avg(n),
  max(n),
  min(n)
from Table1

this returns a single row, multi-column result.

How can I get it into a result that's something like this:

Label | Res
-------------
min   | 2
max   | 3
count | 30
... etc ...
1

There are 1 answers

0
Vao Tsun On

Postgres does not have a pivot trasformation...

    select 
      'sum' as "key",sum(n) as "value" from Table1
    union all
   select   'avg', avg(n) from Table1
    union all
      select   'max', max(n) from Table1
    union all
      select   'min', min(n) from Table1

or if you have superuser can try CREATE EXTENSION tablefunc