Thread: how to: refer to select list calculations other places in the calculations.

how to: refer to select list calculations other places in the calculations.

From
"Little, Douglas"
Date:
<div class="Section1"><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">Hi,</span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">I have very complex sql (generated by cognos) I’m porting from Teradata that basically uses column
aliasin the calculated columns. </span></font><p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size:10.0pt;
font-family:Arial">Is there any way to do this type of thing in Psql?</span></font><p class="MsoNormal"><font
face="Arial"size="2"><span style="font-size:10.0pt; 
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">Essentially I need to do is refer to a calculated column later in the select list (but far more
complex– like 50 pages printed out)   </span></font><p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size:10.0pt;
font-family:Arial">sel a,b,a+b as c, c*d, c*f, c*g, c*h, c*I, c*j, c*k,……       ---  where c is a calculated column
usedin other calculations.</span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; 
font-family:Arial">from ....</span></font><p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size:10.0pt;
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial"> </span></font><p class="MsoNormal"><strong><b><font face="Arial" size="2"><span style="font-size:
10.0pt;font-family:Arial">Doug Little</span></font></b></strong><p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size:10.0pt;
font-family:Arial">Sr. Data Warehouse Architect | Enterprise Data Management | Orbitz Worldwide </span></font><p
class="MsoNormal"style="margin-right:-1.25in"><font face="Arial" size="2"><span
style="font-size:10.0pt;font-family:Arial">500W. Madison, Suite 1000  Chicago IL 60661| Office 312.260.2588 | Fax
312.894.5164| Cell 847-997-5741</span></font><h3 style="margin:0in;margin-bottom:.0001pt"><strong><b><font face="Arial"
size="2"><spanstyle="font-size:10.0pt;font-family:Arial;font-weight:normal"><a href="mailto:Douglas.Little@orbitz.com"
title="mailto:dlittle@orbitz.com"><b><span
style="font-weight:bold">Douglas.Little@orbitz.com</span></b></a></span></font></b></strong><span
class="MsoHyperlink"><u><fontcolor="blue"></font></u></span></h3><p><font face="Times New Roman" size="3"><span
style="font-size:12.0pt"> <imgborder="0" height="61" id="_x0000_i1025" src="cid:image002.jpg@01CA3127.61E69F30"
width="83"/>  <a href="http://www.orbitz.com/" title="http://www.orbitz.com/"><font face="Arial" size="1"><span
style="font-size:
8.0pt;font-family:Arial"><span title="http://www.orbitz.com/">orbitz.com</span></span></font></a> <font
color="blue"><spanstyle="color:blue">|</span></font> <a href="http://www.ebookers.com/"
title="http://www.ebookers.com/"><fontface="Arial" size="1"><span style="font-size:8.0pt;font-family:Arial"><span
title="http://www.ebookers.com/">ebookers.com</span></span></font></a><font color="blue"><span
style="color:blue">|</span></font><a href="http://www.hotelclub.com/" title="http://www.hotelclub.com/"><font
face="Arial"size="1"><span style="font-size:8.0pt;font-family:Arial"><span
title="http://www.hotelclub.com/">hotelclub.com</span></span></font></a><font color="blue"><span
style="color:blue">|</span></font><a href="http://www.cheaptickets.com/" title="http://www.cheaptickets.com/"><font
face="Arial"size="1"><span style="font-size:8.0pt;font-family:Arial"><span
title="http://www.cheaptickets.com/">cheaptickets.com</span></span></font></a><font color="blue"><span
style="color:blue">|</span></font><a href="http://www.ratestogo.com/" title="http://www.ratestogo.com/"><font
face="Arial"size="1"><span style="font-size:8.0pt;font-family:Arial"><span
title="http://www.ratestogo.com/">ratestogo.com</span></span></font></a><fontcolor="blue"><span style="color:blue">
|</span></font><a href="http://www.asiahotels.com/" title="http://www.asiahotels.com/"><font face="Arial"
size="1"><spanstyle="font-size:8.0pt;font-family:Arial"><span
title="http://www.asiahotels.com/">asiahotels.com</span></span></font></a> </span></font><pclass="MsoNormal"><font
face="TimesNew Roman" size="3"><span style="font-size: 
12.0pt"> </span></font></div>

Re: how to: refer to select list calculations other places in the calculations.

From
Rob Sargent
Date:
I might do the substitution, analyse the query and hope to break it up. 
Can it possibly be optimal?

Little, Douglas wrote:
> Hi Rob, 
> Thanks for the response.
> Repeating the base calculation will work but the calculations are  stunningly complex and long.  Repeating them will
undoubtlyincrease editing errors and maintenance problems. 
 
>
> Wish I had a sample to post.  Last query I analyzed was approx 10k lines and printed to over 50 pages.   Yes, it was
asingle query.  I'm amazed that cognos would generate.  Basically they generate a pivoted cross-tab from sql.
 
>
> Thanks
> Doug
>
>
>
>
> -----Original Message-----
> From: Rob Sargent [mailto:robjsargent@gmail.com] 
> Sent: Wednesday, September 09, 2009 11:59 PM
> To: Little, Douglas
> Subject: Re: [SQL] how to: refer to select list calculations other places in the calculations.
>
> Would reverting to the base columns work?
>
> select a, b, a+b as c, (a+b)*d, (a+b)*f etc.
>
> Little, Douglas wrote:
>   
>> Hi,
>>
>> I have very complex sql (generated by cognos) I'm porting from 
>> Teradata that basically uses column alias in the calculated columns.
>>
>> Is there any way to do this type of thing in Psql?
>>
>> Essentially I need to do is refer to a calculated column later in the 
>> select list (but far more complex - like 50 pages printed out)
>>
>> sel a,b,a+b as c, c*d, c*f, c*g, c*h, c*I, c*j, c*k,...... --- where c is 
>> a calculated column used in other calculations.
>>
>> from ....
>>
>> **Doug Little**
>>
>> Sr. Data Warehouse Architect | Enterprise Data Management | Orbitz 
>> Worldwide
>>
>> 500 W. Madison, Suite 1000 Chicago IL 60661| Office 312.260.2588 | Fax 
>> 312.894.5164 | Cell 847-997-5741
>>
>>
>>       ***Douglas.Little@orbitz.com* <mailto:Douglas.Little@orbitz.com>**__
>>
>> orbitz.com <http://www.orbitz.com/> | ebookers.com 
>> <http://www.ebookers.com/> | hotelclub.com <http://www.hotelclub.com/> 
>> | cheaptickets.com <http://www.cheaptickets.com/> | ratestogo.com 
>> <http://www.ratestogo.com/> | asiahotels.com <http://www.asiahotels.com/>
>>
>>     


Re: how to: refer to select list calculations other places in the calculations.

From
Bernd.Nawothnig@t-online.de (Bernd Nawothnig)
Date:
On Wed, Sep 09, 2009 at 08:27:35AM -0500, Little, Douglas wrote:

> I have very complex sql (generated by cognos) I'm porting from
> Teradata that basically uses column alias in the calculated columns.
> Is there any way to do this type of thing in Psql?

I use nested subqueries and do the renaming of calculated columns in the
innermost queries in such cases.




Bernd


Re: how to: refer to select list calculations other places in the calculations.

From
the6campbells
Date:
SQL (unlike MDX) does not allow you to define an expressions and reference it elsewhere in the same block by name. As suggested, a hand written or auto-generated statement can try to refactor the statement by pushing things into a derived table and the outer blocks refer to the projected item names. Not only can this make the SQL more readable it can assist some vendors who internally may not do a great job of common expression factoring. This is one reason why common table expressions are used.
 
For example, if you are in report studio, you can copy the data items into another Bi-Query which you reference from the query that consumes it.


 
On Sat, Oct 31, 2009 at 4:11 PM, Bernd Nawothnig <Bernd.Nawothnig@t-online.de> wrote:
On Wed, Sep 09, 2009 at 08:27:35AM -0500, Little, Douglas wrote:

> I have very complex sql (generated by cognos) I'm porting from
> Teradata that basically uses column alias in the calculated columns.
> Is there any way to do this type of thing in Psql?

I use nested subqueries and do the renaming of calculated columns in the
innermost queries in such cases.




Bernd

--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql