我有这样的事情SQL服务器>查询>列名
Select A.a, A.b, A.a+A.b as c, (A.a+A.b*2)+A.d as d from Table
,但我想知道,如果有可能,以使其与这样的工作:
Select A.a,A.b,A.a+A.b as c, (c*2)+A.d as d from Table
谢谢你
我有这样的事情SQL服务器>查询>列名
Select A.a, A.b, A.a+A.b as c, (A.a+A.b*2)+A.d as d from Table
,但我想知道,如果有可能,以使其与这样的工作:
Select A.a,A.b,A.a+A.b as c, (c*2)+A.d as d from Table
谢谢你
列别名不可用在选择的其他列中。你可以重写使用内嵌视图:
select a, b, c, (c*2)+d as d
from (select A.a, A.b, A.a+A.b as c, A.d as d from Table A) table_alias_for_view
不,你不能这样做,但你最好的选择是使用computed column
@pojomx:
您可以使用计算列,如果你不希望保留在查询计算值:
例如:
create table orders
(
product varchar(50) not null,
qty int not null,
unit_price money not null,
amount as qty * unit_price persisted
);
insert into orders(product,qty,unit_price) values('KEYBOARD',5, 20);
select * from orders;
输出:
product qty unit_price amount
KEYBOARD 5 20.00 100.00
不能在SQL Server中,你必须做像你的第一个例子:
Select A.a, A.b, A.a+A.b as c, (A.a+A.b*2)+A.d as d from Table
或使用派生表所示:
SELECT
dt.a, dt.b,dt.c, dt.c*2+dt.a as d
FROM (Select
A.a, A.b, A.a+A.b as c
from Table
) dt
我在想这个......谢谢=) – pojomx 2010-04-29 17:28:15