我写了一段统计SQL 执行没问题select SITE_NO,BM,NAME,
sum (case when LD_type='业务A' then JF else 0 end) 业务A,
sum (case when LD_type='业务B' then JF else 0 end) 业务B,
sum (case when LD_type='业务C' then JF else 0 end) 业务C,
sum (case when LD_type='业务D' then JF else 0 end) 业务D
from temp_A
where date >= to_date('2011-03-01 00:00:00','yyyy-mm-dd hh24:mi:ss')
group by SITE_NO,BM,NAME;可是为什么出来的数据 业务A,业务B,业务C,业务D 之和
却不等于 直接 select sum(JF ) from temp_AtempA表中除了4种业务 再无别的类型, 麻烦各位高人指点 谢谢
sum (case when LD_type='业务A' then JF else 0 end) 业务A,
sum (case when LD_type='业务B' then JF else 0 end) 业务B,
sum (case when LD_type='业务C' then JF else 0 end) 业务C,
sum (case when LD_type='业务D' then JF else 0 end) 业务D
from temp_A
where date >= to_date('2011-03-01 00:00:00','yyyy-mm-dd hh24:mi:ss')
group by SITE_NO,BM,NAME;可是为什么出来的数据 业务A,业务B,业务C,业务D 之和
却不等于 直接 select sum(JF ) from temp_AtempA表中除了4种业务 再无别的类型, 麻烦各位高人指点 谢谢
--指定的时间段问题,
--看看下面这两条语句,查询出来的应该相等的!
select SITE_NO,BM,NAME,
sum (case when LD_type='业务A' then JF else 0 end) 业务A,
sum (case when LD_type='业务B' then JF else 0 end) 业务B,
sum (case when LD_type='业务C' then JF else 0 end) 业务C,
sum (case when LD_type='业务D' then JF else 0 end) 业务D
from temp_A
where date >= to_date('2011-03-01 00:00:00','yyyy-mm-dd hh24:mi:ss')
group by SITE_NO,BM,NAME;select sum(JF ) from temp_A
where date >= to_date('2011-03-01 00:00:00','yyyy-mm-dd hh24:mi:ss');
数据源都是同一个表 tempA. 不会有这种情况把?
SQL> select deptno,sum(sal)
2 from emp
3 group by deptno;DEPTNO SUM(SAL)
------ ----------
10 8750
20 10875
30 9400SQL> select sum(sal)
2 from emp; SUM(SAL)
----------
29025
--
8750+10875+9400=29025
我直接group by LD_type 也是对的,是不是case when?这里有问题?