SQL多表查询语句修改

hehaiyi2008 2010-09-16 03:34:28
请问,以下SQL语句的NULL 值的问题,如何处理比较好!



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 操作消除了空值。




【正确的结果应该是】:

产品名 基础 购进 返回 分发 扣除 金额
---------- ----------- ----------- ----------- ----------- ----------- -----------
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



请高手帮忙修改此SQL 语句。谢谢!~~~
...全文
269 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ws_hgo 2010-09-16
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hehaiyi2008 的回复:]
引用 5 楼 ws_hgo 的回复:

3楼的不是你要的结果吗》

如果不是
你要的结果是?


正确的结果是:
产品名 基础 购进 返回 分发 扣除 金额
---------- ----------- ----------- ----------- ----------- ----------- -----------
CP1 100 170 120 20 10 3……
[/Quote]

明白了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 操作消除了空值。
hehaiyi2008 2010-09-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ws_hgo 的回复:]

3楼的不是你要的结果吗》

如果不是
你要的结果是?
[/Quote]

正确的结果是:
产品名 基础 购进 返回 分发 扣除 金额
---------- ----------- ----------- ----------- ----------- ----------- -----------
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
ws_hgo 2010-09-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hehaiyi2008 的回复:]
引用 2 楼 ws_hgo 的回复:

不是我写的吗


还是不对!
[/Quote]
3楼的不是你要的结果吗》

如果不是
你要的结果是?
hehaiyi2008 2010-09-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ws_hgo 的回复:]

不是我写的吗
[/Quote]

还是不对!
ws_hgo 2010-09-16
  • 打赏
  • 举报
回复
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 行受影响)
ws_hgo 2010-09-16
  • 打赏
  • 举报
回复

不是我写的吗
xiaoxiao8372 2010-09-16
  • 打赏
  • 举报
回复
isnull

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧