Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不带子查询的SQL列选择别名_Sql_Sql Server 2008 - Fatal编程技术网

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

不带子查询的SQL列选择别名,sql,sql-server-2008,Sql,Sql Server 2008,在sql 2008中,是否可以在不使用子查询的情况下选择列别名 *****我想做的是:****** Select col1*col2 as NewColumn, NewColumn*col3 from table 但当我尝试此操作时,会出现以下错误: 列名“NewColumn”无效 ****我现在正在做的是****** Select Newcolumn*col3 from (Select col1*col2, col3 from mytable) Q1 我希望尽可能避免子查询,因为我要连接许

在sql 2008中,是否可以在不使用子查询的情况下选择列别名

*****我想做的是:******

Select col1*col2 as NewColumn, NewColumn*col3 from table
但当我尝试此操作时,会出现以下错误:

列名“NewColumn”无效

****我现在正在做的是******

Select Newcolumn*col3 from (Select col1*col2, col3 from mytable) Q1
我希望尽可能避免子查询,因为我要连接许多表,并希望使查询更具可读性

也许有更好的方法可以做到这一点


提前谢谢

您可以使用子查询或CTE。或者,如果您真的需要,
外部应用

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

既然你可以很容易地做到这一点,你为什么要把它弄得这么复杂

试试这个

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

希望这会有所帮助。

使用子查询或CTE。@Rdisnic。您的请求是避免子查询以保持查询的可读性。在
FROM
子句中再添加一个表达式与附加嵌套级别不同。