Can't use column aliases in the calculation of derived colum

Moderator: NorbertKrupa

maheshsanapala
Newbie
Newbie
Posts: 4
Joined: Wed Jun 04, 2014 6:39 am

Can't use column aliases in the calculation of derived colum

Post by maheshsanapala » Wed Jul 30, 2014 8:34 am

Hi,

When i run sample query like below, it says Column1 NOT FOUND. However, i can rewrite the same thing by using sub query and it works fine too but it's quite painful when i write big queries. Doesn't VERTICA support this? OR i'm missing something? Any help would be greatly appreciated.

SELECT a+b AS column1
Column2,
Column1/Column2 AS Column3,
CASE WHEN Column3=0 THEN 0 ELSE Column2/Column3 END AS Column4
FROM
Vertica_Table;

Thanks
Mahesh.

NorbertKrupa
GURU
GURU
Posts: 527
Joined: Tue Oct 22, 2013 9:36 pm
Location: Chicago, IL
Contact:

Re: Can't use column aliases in the calculation of derived c

Post by NorbertKrupa » Wed Jul 30, 2014 4:04 pm

I would be surprised to see what DBMS actually supports that because of the way that fields are evaluated in the SELECT statement. You could explicitly define the calculation for your aliased columns, or use a subquery:

Code: Select all

SELECT CASE 
         WHEN ( ( a + b ) / column2 ) = 0 THEN 0 
         ELSE column2 / ( ( a + b ) / column2 ) 
       END AS Column4 
FROM   vertica_table; 

Code: Select all

SELECT CASE 
         WHEN column1 / column2 = 0 THEN 0 
         ELSE column2 / ( column1 / column2 ) 
       END AS column4 
FROM   (SELECT a + b AS column1, 
               column2 
        FROM   vertica_table) AS a;
Checkout vertica.tips for more Vertica resources.

maheshsanapala
Newbie
Newbie
Posts: 4
Joined: Wed Jun 04, 2014 6:39 am

Re: Can't use column aliases in the calculation of derived c

Post by maheshsanapala » Thu Jul 31, 2014 3:45 am

Teradata supports this. It's an existing code that has been working since ages. We are trying to migrate to Vertica due to performance issues and it does not work in vertica.

NorbertKrupa
GURU
GURU
Posts: 527
Joined: Tue Oct 22, 2013 9:36 pm
Location: Chicago, IL
Contact:

Re: Can't use column aliases in the calculation of derived c

Post by NorbertKrupa » Thu Jul 31, 2014 5:26 am

Well then; I now know of one platform that supports this.

While it would be easy for me to blast Teradata, I would rather put the blame on the individual that wrote the query. It's not ANSI standard SQL and just because Teradata extends functionality to evaluate columns more than once in the SELECT clause doesn't make it good practice.
Checkout vertica.tips for more Vertica resources.

maheshsanapala
Newbie
Newbie
Posts: 4
Joined: Wed Jun 04, 2014 6:39 am

Re: Can't use column aliases in the calculation of derived c

Post by maheshsanapala » Thu Jul 31, 2014 7:02 am

Well, i got my answer. Vertica does not support this. Thanks for your response.

id10t
GURU
GURU
Posts: 732
Joined: Mon Apr 16, 2012 2:44 pm

Re: Can't use column aliases in the calculation of derived c

Post by id10t » Thu Jul 31, 2014 8:11 am

Hi!

[DELETED]
Last edited by id10t on Wed May 06, 2015 5:07 pm, edited 1 time in total.

maheshsanapala
Newbie
Newbie
Posts: 4
Joined: Wed Jun 04, 2014 6:39 am

Re: Can't use column aliases in the calculation of derived c

Post by maheshsanapala » Thu Jul 31, 2014 8:26 am

Hi Skwa,

As mentioned before, Teradata supports that kind of SQL .

Thanks
Mahesh

Post Reply

Return to “General”