Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

Thanks so much for having a place for us propeller heads to hang out and chat.

Geography

Where in the world do Tek-Tips members come from?

How to create a pivot table in PostgreSQL?

metaltree (TechnicalUser)
22 Jan 08 14:28
Suppose I have the 'abc' table:

individual year  size
1_4_5      1     112
1_4_5      2     123
1_4_5      3     146
1_4_5      4     157
2_8_3      1     92
2_8_3      2     104
2_8_3      3     98
...

I want to pivot the table to get:

individual size_yr1   size_yr2   size_yr3   ...
1_4_5      112        123        146
2_8_3      92         104        98
...

In Access, you would do:

TRANSFORM Avg(size) AS size_avg
SELECT abc.individual
FROM abc
GROUP BY abc.individual
PIVOT abc.year;

But this is MS-specific SQL...

Is there a simple way to do it in PostgreSQL?


postgresql007 (Programmer)
27 Jan 08 5:51
You can use a function called "crosstab" to do that.
It is part of the PostgreSQL contrib-section and it has to be installed separately. Checkout the "contrib/tablefunc" directory inside your sourceball.

Hans-Jürgen Schönig
Cybertec Schönig & Schönig GmbH
URL: http://www.postgresql-support.de
PostgreSQL Support, Training, Replication

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close