求一SqlServer语句
[A表]
id BBB
5 10
6 15
[B表]
id BBB
6 5
我要的结果是当[A表] id = [B表] id 的时候 [A表] BBB + [B表] BBB
如果[B表] id 没有包含[A表] id的时候只取[A表] BBB字段的值
要的结果如下:
10
20
------解决方案--------------------select id,sum(bbb)
from (
select id,bbb
from a
union all
select id,bbb
from b)
a
group by id
------解决方案--------------------try this,
select a.id,
a.BBB+isnull(b.BBB,0) 'BBB'
from [A表] a
left join [B表] b on a.id=b.id