>데이터 베이스 >MySQL 튜토리얼 >Oracle子句GROUP BY CUBE续

Oracle子句GROUP BY CUBE续

WBOY
WBOY원래의
2016-06-07 17:17:551120검색

上篇日志《ORACLE子句GROUP BY CUBE》中的问题稍作变化,现在每个字段有3个状态,SQL该如何写呢?这时CUBE的机制恐怕没有办法构成

上篇日志《Oracle子句GROUP BY CUBE》(见  )中的问题稍作变化,现在每个字段有3个状态,SQL该如何写呢?这时CUBE的机制恐怕没有办法构成3的八次方行数据了,只能另寻办法。以下代码是以3列为例的一个比较啰嗦的方案,仅供参考。

with base as
(select 0 as a1, 0 as a2, 0 as a3
   from dual
 union all
 select 1 as a1, 0 as a2, 0 as a3
   from dual
 union all
 select 9 as a1, 0 as a2, 0 as a3 from dual)
select b.a1,decode(c.le,1,0,2,1,3,9) as a2,decode(d.le,1,0,2,1,3,9) as a3
 from base b,
      (select level as le
         from dual
       connect by level
       (select level as le
         from dual
       connect by level
       order by a1,a2,a3
 
惭愧,,有点低级错误啦。修改后代码如下:

with base as
 (select 0 as a1, 0 as a2, 0 as a3
   from dual
 union all
 select 1 as a1, 0 as a2, 0 as a3
   from dual
 union all
 select 9 as a1, 0 as a2, 0 as a3 from dual),
mult as
 (select level as le
   from dual
 connect by level
select b.a1,
      decode(m1.le, 1, 0, 2, 1, 3, 9) as a2,
      decode(m2.le, 1, 0, 2, 1, 3, 9) as a3
 from base b, mult m1, mult m2
 order by a1, a2, a3

linux

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:ORA-600 [4194] 说明다음 기사:ORA-12519错误解决方法