SQL 别名相加

来源:百度知道 编辑:UC知道 时间:2024/05/29 05:07:45
select distinct A.A1,A.A2,
(select distinct B2 from B where A.A1=B.A1) as 列1,
(select distinct C2 from C where A.A1=C.A1) as 列2,
(列1+列2) 列3
from A
我希望能通过别名将两行相加,如上面所示,但是提示出错,请问有什么方法能将这两者相加呢?
B和C那两条语句很长的啊,只有这一种方法吗?

select distinct a1.A1,a1.A2,
a2.B2 as 列1,
a3.c3 as 列2,
(a2.B2 + a3.c3) as 列3
from A a1,
(select distinct B2 from B where A.A1=B.A1) a2,
(select distinct C2 from C where A.A1=C.A1) a3

这样速度稍微快点,效率高点

select distinct A.A1,A.A2,
(select distinct B2 from B where A.A1=B.A1) as 列1,
(select distinct C2 from C where A.A1=C.A1) as 列2,
((select distinct B2 from B where A.A1=B.A1)+(select distinct C2 from C where A.A1=C.A1)) 列3
from A
只能这样