select a.客户ID,客户名称,b.销售数量,b.废品数量 from 客户 a, (select isnull (a.客户id,b.客户id) as 客户id, isnull (a.销售数量,0) as 销售数量, isnull (b.废品数量,0) as 废品数量 from 销售表 a full join 废品表 b on a.客户id=b.客户id) b where a.客户id=b.客户id
select 客户.客户ID,客户.客户名称,temp1.销售数量 as 销售数量,temp2.废品数 as 废品数量 from 客户 left join (select 客户ID,sum(销售数量) as 销售数量 from 销售表 group by 客户ID) temp1 on 客户.客户ID = temp1.客户ID left join (select 客户ID,sun(废品数) as 废品数 from 废品数 group by 客户ID) temp2 on 客户.客户ID = temp2.客户ID
抄袭: skyboy0720(飞) select a.客户ID, a.客户名称, 销售数量 = isnull(( select sum(销售数量) from 销售表 where 客户ID = a.客户ID ), 0), 废品数量= isnull(( select sum(废品数) from 废品数 where 客户ID = a.客户ID ), 0) from 客户 a
from
客户 a,
(select
isnull (a.客户id,b.客户id) as 客户id,
isnull (a.销售数量,0) as 销售数量,
isnull (b.废品数量,0) as 废品数量
from 销售表 a full join 废品表 b
on a.客户id=b.客户id) b
where a.客户id=b.客户id
from 客户
left join
(select 客户ID,sum(销售数量) as 销售数量 from 销售表 group by 客户ID) temp1
on 客户.客户ID = temp1.客户ID
left join
(select 客户ID,sun(废品数) as 废品数 from 废品数 group by 客户ID) temp2
on 客户.客户ID = temp2.客户ID
select a.客户ID, a.客户名称, 销售数量 = isnull(( select sum(销售数量) from 销售表 where 客户ID = a.客户ID ), 0),
废品数量= isnull(( select sum(废品数) from 废品数 where 客户ID = a.客户ID ), 0) from 客户 a