Unpivot / uncrosstab support? - Mailing list pgsql-general

From
Subject Unpivot / uncrosstab support?
Date
Msg-id C237184E7081314392F31AE826947FFA44E19DF94C@EXWA-MBX01.nexus.csiro.au
Whole thread Raw
Responses Re: Unpivot / uncrosstab support?  (<Josh.Vote@csiro.au>)
List 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
theuser 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


pgsql-general by date:

Previous
From: Craig Ringer
Date:
Subject: Re: Full Text Search : Parse date
Next
From: Craig Ringer
Date:
Subject: Re: Server tries to connect without ssl support