求个sql语句
实现功能
"select a.编码,a.名称,(select sum(金额) from b where b.编码=a.编码) as 金额1,(select sum(金额) from c where c.编码=a.编码) as 金额2 from a"
我应该如何实现对嵌套sql的加减运算呢,就是上面的语句,我想在体现个金额3,金额3=金额1-金额2,怎么在一天语句中实现呢,有什么好方法没?
[解决办法]
select a.编码,a.名称,
(select sum(金额) from b where b.编码=a.编码) as 金额1,
(select sum(金额) from c where c.编码=a.编码) as 金额2,
(select sum(金额) from b where b.编码=a.编码)-(select sum(金额) from c where c.编码=a.编码) as 金额2
from a
select a.编码,a.名称,sum(b.金额) as 金额1,sum(c.金额) as 金额2,
sum(isnull(b.金额,0))- sum(isnull(c.金额,0)) as 金额3
from a
left join b on b.编码=a.编码
left join c on c.编码=a.编码
group by a.编码,a.名称