SQL union all 问题,高手进入!!!

huerlin 2015-10-08 11:41:50
下面这两个SQL有区别吗,为什么统计的结果确不一样

SELECT sum(T.je)
FROM (
select je=sum(je) from table1 where sj>='2015-08-31 18:00:00' AND sj<='2015-09-30 18:00:00'
union all
select je=sum(tab.je)
from (
select je=sum(table2.je) from table2 where sj>='2015-08-31 18:00:00' AND sj<='2015-09-30 18:00:00' and zfbj=0
union all
select je= 0 - sum(table2.je) from table2 where sj>='2015-08-31 18:00:00' AND sj<='2015-09-30 18:00:00' and zfbj=1
) as tab
) as T


---------------------------------------------------------------------------------------


SELECT sum(T.je)
FROM (
select je=sum(je) from table1 where sj>='2015-08-31 18:00:00' AND sj<='2015-09-30 18:00:00'
union all
select je=sum(table2.je) from table2 where sj>='2015-08-31 18:00:00' AND sj<='2015-09-30 18:00:00' and zfbj=0
union all
select je= 0 - sum(table2.je) from table2 where sj>='2015-08-31 18:00:00' AND sj<='2015-09-30 18:00:00' and zfbj=1
) as T
...全文
207 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
卖水果的net 版主 2015-10-09
  • 打赏
  • 举报
回复
引用 5 楼 Leftie 的回复:
会不会是有NULL的原因,这样试试:sum(isnull(字段名,0))
sum 会忽略NULL。
饮水需思源 2015-10-09
  • 打赏
  • 举报
回复
会不会是有NULL的原因,这样试试:sum(isnull(字段名,0))
道玄希言 2015-10-08
  • 打赏
  • 举报
回复
没区别的。 sum(T.je) 是 sum(je) 和 sum(tab.je) 的和, 而 sum(tab.je) 就是 sum(table2.je) 和 (0 - sum(table2.je) )的和。 第二个, sum(T.je) 就是 sum(je), sum(table2.je), ( 0 - sum(table2.je) ) 三者之和了。
卖水果的net 版主 2015-10-08
  • 打赏
  • 举报
回复

-- 确认一下两张表的 je 列,数据类型是否完全一样,
-- 给你一个不一样的例子

create table table1(je decimal(18,0) , type int)
go
create table table2(je decimal(18,2) , type int)
go
insert into table1 values(100,1),(200,1)
go
insert into table2 values(1.5 , 1), (1.3,1) , (1.4,2)
go
select * from table1
go
select * from table2 
go
select sum(t.je) from(
    select sum(je) je from table1 
    union all
    select sum(je) je from( 
      select sum(table2.je) je from table2 where type = 1 
      union all
      select 0-sum(table2.je) je from table2 where type = 2
    ) tab
) t
go
select sum(t.je) from
(
select sum(je) je from table1 
union all
select sum(table2.je) je from table2 where type = 1 
union all
select 0-sum(table2.je) je from table2 where type = 2
) t
go
drop table table1,table2
go


(2 行受影响)

(3 行受影响)
je                                      type
--------------------------------------- -----------
100                                     1
200                                     1

(2 行受影响)

je                                      type
--------------------------------------- -----------
1.50                                    1
1.30                                    1
1.40                                    2

(3 行受影响)


---------------------------------------
301

(1 行受影响)


---------------------------------------
302

(1 行受影响)



Neo_whl 2015-10-08
  • 打赏
  • 举报
回复
没啥区别,你所指的是数据精确度不同还是记录条数的差异?按道理是一样的
huerlin 2015-10-08
  • 打赏
  • 举报
回复
是啊,我看也没有区别,但统计出来的数据就不一样! 环境是SQL2005 sp4的

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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