Unpivot / uncrosstab support?

From: <Josh(dot)Vote(at)csiro(dot)au>
To: <pgsql-general(at)postgresql(dot)org>
Subject: Unpivot / uncrosstab support?
Date: 2010-05-13 02:41:14
Message-ID: C237184E7081314392F31AE826947FFA44E19DF94C@EXWA-MBX01.nexus.csiro.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

I was wondering if Postgresql (8.3 or later) had an equivalent function to the MS-SQL function "unpivot"? I've spotted the user contributed crosstab which is similar to the "pivot" function, but I can't find an equivalent "uncrosstab".

Essentially what I'm trying to do is take a table like this...

clientID clientName contact1 contact2 contact3 contact4
----------- -------------------- ----------- ----------- ----------- -----------
1 ABC Corp 1 34 2 NULL
2 DEF Foundation 6 2 8 9
3 GHI Inc. 5 9 NULL NULL
4 XYZ Industries 24 NULL 6 NULL

...and get a result like this...

clientID ContactNumber ContactID
----------- ----------------------- -----------
1 contact1 1
1 contact2 34
1 contact3 2
2 contact1 6
2 contact2 2
2 contact3 8
2 contact4 9
3 contact1 5
3 contact2 9
4 contact1 24
4 contact3 6

Which can be accomplished with unpivot.

For more info I've stolen the example from http://weblogs.sqlteam.com/jeffs/archive/2008/04/23/unpivot.aspx

Thanks
Josh Vote

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Craig Ringer 2010-05-13 02:52:56 Re: Server tries to connect without ssl support
Previous Message Craig Ringer 2010-05-13 02:31:17 Re: Full Text Search : Parse date