Thread: crosstab

crosstab

From
Theo Galanakis
Date:
<p><font size="2">Hi I have looked into the tablefunc / crosstab contrib for postgres and it appears like it can't
performwhat I need.</font><p><font size="2">The crosstab function converts this : </font><p>                <font
size="2">row_name    cat    value</font><br /><font size="2">            ----------+-------+-------</font><br /><font
size="2">             row1      cat1    val1</font><br /><font size="2">              row1      cat2    val2</font><br
/><fontsize="2">              row1      cat3    val3</font><br /><font size="2">              row1      cat4   
val4</font><br/><font size="2">              row2      cat1    val5</font><br /><font size="2">              row2     
cat2   val6</font><br /><font size="2">              row2      cat3    val7</font><br /><font size="2">             
row2     cat4    val8</font><p><font size="2">To this : </font><p>                <font size="2">row_name  
category_1  category_2</font><br /><font size="2">           ---------+------------+------------</font><br /><font
size="2">            row1        val1         val2</font><br /><font size="2">             row2        val5        
val6</font><p><fontsize="2">Is it possible to do the opposite and go from a column(denormalized) structure to a
row(normalized)structure.</font><p><font size="2">I have a table that is similarly stuctured like so:</font><p><font
size="2">Melbourne_figures              Sydney_figures  Adelaide_figures   etc...</font><br />        <font
size="2">10                     20                      22</font><br />        <font size="2">10                     
22                     29</font><br />        <font size="2">...</font><br /><p><font size="2">However I wish to
convertlike so :</font><p><font size="2">Melbourne       20</font><br /><font size="2">Sydney  42</font><br /><font
size="2">Adelaide       51</font><p><font size="2">I have tried using unions or subselects however the table is quite
largeand it takes far too long to run. The most efficient way would be to create a stored proc that uses a cursor to
loopthrough the table transforming the data into the new table structure. However I would appreciate your feeback
beforewriting this procedure?</font><p><font size="2">Theo</font><table><tr><td bgcolor="#ffffff"><font
color="#000000">______________________________________________________________________<br/> This email, including
attachments,is intended only for the addressee<br /> and may be confidential, privileged and subject to copyright. If
you<br/> have received this email in error, please advise the sender and delete<br /> it. If you are not the intended
recipientof this email, you must not<br /> use, copy or disclose its content to anyone. You must not copy or <br />
communicateto others content that is confidential or subject to <br /> copyright, unless you have the consent of the
contentowner.<br /></font></td></tr></table> 

Re: crosstab

From
Josh Berkus
Date:
THeo,

> I have tried using unions or subselects however the table is quite large
> and it takes far too long to run. The most efficient way would be to create
> a stored proc that uses a cursor to loop through the table transforming the
> data into the new table structure. However I would appreciate your feeback
> before writing this procedure?

A proc won't run any faster ... much slower, in fact.    Unless you mean that 
you want to use it to correct the actual table structure, which is what you 
should be doing?

-- 
--Josh

Josh Berkus
Aglio Database Solutions
San Francisco