我有這樣的事情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