22,209
社区成员
发帖
与我相关
我的任务
分享
if object_id('table1') is not null drop table table1
create table table1
(
产品名 varchar(10),
基础 int
)
insert into table1 select 'CP1',100
union all select 'TB1',200
union all select 'DD1',20
union all select 'ED1',0
if object_id('table2') is not null drop table table2
create table table2
(
产品名 varchar(10),
项目 varchar(20),
金额 int
)
insert into table2 select 'CP1','购进',50
union all select 'CP1','返回',120
union all select 'TB1','购进',20
union all select 'TB1','返回',30
union all select 'CP1','购进',60
union all select 'CP1','购进',60
if object_id('table3') is not null drop table table3
create table table3
(
产品名 varchar(10),
项目 varchar(20),
金额 int
)
insert into table3 select 'CP1','分发',20
union all select 'CP1','扣除',10
union all select 'TB1','扣除',10
union all select 'TB1','扣除',20
select b1.产品名,sum(b1.基础) 基础,
sum(b2.购进) 购进,sum(b2.返回) 返回,
sum(b3.分发) 分发,sum(b3.扣除) 扣除,
sum(b1.基础)+sum(金额2)-sum(金额3) 金额
from table1 b1
left join
(
select 产品名,
sum(case when 项目='购进' then 金额 else 0 end) '购进',
sum(case when 项目='返回' then 金额 else 0 end) '返回',
sum(金额) 金额2
from table2 group by 产品名
) b2
on b1.产品名=b2.产品名
left join
(
select 产品名,
sum(case when 项目='分发' then 金额 else 0 end) '分发',
sum(case when 项目='扣除' then 金额 else 0 end) '扣除',
sum(金额) 金额3
from table3 group by 产品名
) b3
on b1.产品名=b3.产品名
group by b1.产品名
产品名 基础 购进 返回 分发 扣除 金额
---------- ----------- ----------- ----------- ----------- ----------- -----------
CP1 100 170 120 20 10 360
DD1 20 NULL NULL NULL NULL NULL
ED1 0 NULL NULL NULL NULL NULL
TB1 200 20 30 0 30 220
(所影响的行数为 4 行)
警告: 聚合或其它 SET 操作消除了空值。
明白了DD1没有总金额
select b1.产品名,isnull(sum(b1.基础),0) 基础,
isnull(sum(b2.购进),0) 购进,isnull(sum(b2.返回),0) 返回,
isnull(sum(b3.分发),0) 分发,isnull(sum(b3.扣除),0) 扣除,
sum(b1.基础)+isnull(sum(金额2),0)-isnull(sum(金额3),0) 金额
from table1 b1
left join
(
select 产品名,
sum(case when 项目='购进' then 金额 else 0 end) '购进',
sum(case when 项目='返回' then 金额 else 0 end) '返回',
sum(金额) 金额2
from table2 group by 产品名
) b2
on b1.产品名=b2.产品名
left join
(
select 产品名,
sum(case when 项目='分发' then 金额 else 0 end) '分发',
sum(case when 项目='扣除' then 金额 else 0 end) '扣除',
sum(金额) 金额3
from table3 group by 产品名
) b3
on b1.产品名=b3.产品名
group by b1.产品名
产品名 基础 购进 返回 分发 扣除 金额
---------- ----------- ----------- ----------- ----------- ----------- -----------
CP1 100 170 120 20 10 360
DD1 20 0 0 0 0 20
ED1 0 0 0 0 0 0
TB1 200 20 30 0 30 220
警告: 聚合或其他 SET 操作消除了空值。
select b1.产品名,isnull(sum(b1.基础),0) 基础,
isnull(sum(b2.购进),0) 购进,isnull(sum(b2.返回),0) 返回,
isnull(sum(b3.分发),0) 分发,isnull(sum(b3.扣除),0) 扣除,
isnull(sum(b1.基础)+sum(金额2)-sum(金额3),0) 金额
from table1 b1
left join
(
select 产品名,
sum(case when 项目='购进' then 金额 else 0 end) '购进',
sum(case when 项目='返回' then 金额 else 0 end) '返回',
sum(金额) 金额2
from table2 group by 产品名
) b2
on b1.产品名=b2.产品名
left join
(
select 产品名,
sum(case when 项目='分发' then 金额 else 0 end) '分发',
sum(case when 项目='扣除' then 金额 else 0 end) '扣除',
sum(金额) 金额3
from table3 group by 产品名
) b3
on b1.产品名=b3.产品名
group by b1.产品名
产品名 基础 购进 返回 分发 扣除 金额
---------- ----------- ----------- ----------- ----------- ----------- -----------
CP1 100 170 120 20 10 360
DD1 20 0 0 0 0 0
ED1 0 0 0 0 0 0
TB1 200 20 30 0 30 220
警告: 聚合或其他 SET 操作消除了空值。
(4 行受影响)