简体   繁体   English

不带子查询的 SQL 列选择别名

[英]SQL column select alias without subquery

Is it possible to select a column alias without using subqueries in sql 2008?是否可以在不使用 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'无效的列名“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 .您可以使用子查询或 CTE 。 . . . . Or, if you really want, outer apply :或者,如果你真的想要, 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.希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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