Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Alias in SELECT statement

Author  Topic 

ITTrucker
Yak Posting Veteran

64 Posts

Posted - 2012-02-02 : 11:03:19
Is there a way to refer to an alias in the SELECT part of a query?

I have a query that is building on itself and it's working, but I was trying to clean it up to make it more readable. It starts off small, but it gets lengthy at the end with subselects and including the code from previous line. Example of how I was hoping it could work (or something...):

SELECT
[FSC] = CEILING((A.fuel_price - 1.20)/.06)/100,
[FSC_CHARGE] = [FSC] * H.freight_miles,
[TRUE LH] = H.LH_charge - [FSC_CHARGE],
etc

The last line of the query involves 6 of the previous calculations and if I could reference the aliases in the select it would shorten it significantly.

There are a ton of posts about not using aliases in the where or group by clauses, but I can't fine anything about using them in the select section only.

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-02-02 : 11:32:00
generally, you'd use derived tables. Do the calculation and date retrieval once and then, alias each column.

Then refer to your inner query's aliases to do additional calculations.

Post the whole query maybe?

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -