17,140
社区成员




--你的报错修改,下面的你运行就不会报错了
select distinct t1.lineid,'' as linesum,t2.speedlevel,
decode(t2.exceptionitem,0,t2.level4count,0) as a
from pw_run_record t1,pw_detect_exceptionstatic t2,pw_detect_tqistatic t3
where t1.detectid=t2.detectid and t1.detectid=t3.detectid
union all
select distinct t1.lineid,'合计' as linesum,t2.speedlevel,sum(decode(t2.exceptionitem,0,t2.level4count,0)) as a
from pw_run_record t1,pw_detect_exceptionstatic t2,pw_detect_tqistatic t3
where t1.detectid=t2.detectid and t1.detectid=t3.detectid
group by t1.lineid,t2.speedlevel
order by lineid,speedlevel
--试试粒度统计
select t1.lineid,
decode(grouping(t1.lineid)+grouping(t2.speedlevel),2,'总计',1,'小计',0,t2.speedlevel)
sum(decode(t2.exceptionitem,0,t2.level4count,0)) as a
from pw_run_record t1,pw_detect_exceptionstatic t2,pw_detect_tqistatic t3
where t1.detectid=t2.detectid and t1.detectid=t3.detectid
group by rollup(t1.lineid,t2.speedlevel)
order by t1.lineid,t2.speedlevel
-----使用union all 是先组合,然后再排序列,所以你使用的 order by t1.lineid, t2.speedlevel并不是来自t1,和t2表
-----而是来自union all之后的表,所以你必须这样写,就必须加上我加的select * from
select distinct t1.lineid,
'' as linesum,
t2.speedlevel,
sum(decode(t2.exceptionitem, 0, t2.level4count, 0)) as a
from pw_run_record t1,
pw_detect_exceptionstatic t2,
pw_detect_tqistatic t3
where t1.detectid = t2.detectid
and t1.detectid = t3.detectid
union all
select *
from (select distinct t1.lineid,
'合计' as linesum,
t2.speedlevel,
sum(decode(t2.exceptionitem, 0, t2.level4count, 0)) as a
from pw_run_record t1,
pw_detect_exceptionstatic t2,
pw_detect_tqistatic t3
where t1.detectid = t2.detectid
and t1.detectid = t3.detectid
group by t1.lineid, t2.speedlevel
order by t1.lineid, t2.speedlevel)
--
SELECT CASE
WHEN deptno IS NULL THEN '合计'
WHEN deptno IS NOT NULL AND empno IS NULL THEN '小计'
ELSE ''||deptno
END deptno,
empno,
ename,
SUM(sal) total_sal
FROM emp
GROUP BY GROUPING SETS((deptno),(deptno, empno, ename),());
DEPTNO EMPNO ENAME TOTAL_SAL
10 7782 CLARK 1450
10 7839 KING 5000
10 7934 MILLER 1300
小计 7750
20 7369 SMITH 800
20 7566 JONES 2975
20 7788 SCOTT 3000
20 7876 ADAMS 1100
20 7902 FORD 3000
小计 10875
30 7900 JAMES 950
30 7499 ALLEN 1600
30 7521 WARD 1250
30 7654 MARTIN 1250
30 7698 BLAKE 2850
30 7844 TURNER 1500
30 7955 BLACK 1250
30 7999 MAXTON 11250
小计 21900
合计 40525
select 1 as aa from dual a
order by a.dummy
select 1 as dummy from dual a
union all
select 2 from dual a
order by a.dummy
这样就不行了
select distinct t1.lineid,decode(grouping(t2.speedlevel),1,'合计') as linesum,
t2.speedlevel,sum(decode(t2.exceptionitem,0,t2.level4count,0)) as a from pw_run_record t1,pw_detect_exceptionstatic t2,pw_detect_tqistatic t3 where t1.detectid=t2.detectid and t1.detectid=t3.detectid
group by t1.lineid,rollup(t2.speedlevel)
order by t1.lineid,2,t2.speedlevel
select decode(grouping(t1.lineid),1,'合計',t1.lineid) lineid,t2.speedlevel,sum(decode(t2.exceptionitem,0,t2.level4count,0)) as a from pw_run_record t1,pw_detect_exceptionstatic t2,pw_detect_tqistatic t3
where t1.detectid=t2.detectid
and t1.detectid=t3.detectid
group by rollup ((t1.lineid,t2.speedlevel))
order by lineid,t2.speedlevel
--例如
SQL> with a as (
2 select 'a' A, 'b' B ,5 C ,2 D from dual
3 union all
4 select 'w', 'b', 1, 3 from dual
5 union all
6 select 'x' ,'x' ,3 ,1 from dual
7 )
8 select decode(grouping(A),1,'合計',A) A
9 ,B,sum(C) C,SUM(D) D
10 from a
11 group by rollup ((a,b))
12 /
A B C D
---- - ---------- ----------
a b 5 2
w b 1 3
x x 3 1
合計 9 6