简体   繁体   中英

SQL column select alias without subquery

Is it possible to select a column alias without using subqueries in sql 2008?

***** what I'd like to do: ******

Select col1*col2 as NewColumn, NewColumn*col3 from table

When I try this though I get the error:

Invalid column name 'NewColumn'

****what I am doing now instead *****

Select Newcolumn*col3 from (Select col1*col2, col3 from mytable) Q1

I'd like to avoid subquery where possible as I'm joining many tables and want to make the query more readable.

Perhaps there is an even better way to do this?

Thanks in advance.

You can use a subquery or CTE . . . Or, if you really want, outer apply :

select x.NewColumn, x.NewColumn * col3
from . . . cross apply
     (select col1*col2 as NewColumn) x;

Why are you making this complicated when you can easily do this,

Try this,

Select col1*col2 as NewColumn, col1*col2*col3 as NewColumn2 from table

Hope this will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM