--■■■Eval関数を使う方法(10g以降)■■■
create or replace function eval(in_str varchar2) return number is
willReturn number;
begin
execute immediate 'select ' || in_str || ' from dual' into willReturn;
return willReturn;
end;
/
select EMPNO,eval(RegExp_Replace(to_char(EMPNO),'([0-9])','+\1')) as SUM_ENO,
SAL,eval(RegExp_Replace(to_char(SAL),'([0-9])','+\1')) as SUM_SAL
from WorkTable;
--■■■mod関数を使う方法■■■
select EMPNO,
(mod(EMPNO,10000)-mod(EMPNO,1000))/1000+
(mod(EMPNO,1000) -mod(EMPNO,100)) /100+
(mod(EMPNO,100) -mod(EMPNO,10)) /10+
mod(EMPNO,10) as SUM_ENO,
SAL,
(mod(SAL,10000)-mod(SAL,1000))/1000+
(mod(SAL,1000) -mod(SAL,100)) /100+
(mod(SAL,100) -mod(SAL,10)) /10+
mod(SAL,10) as SUM_SAL
from WorkTable;
--■■■文字列関数を使う方法■■■
select EMPNO,
nvl(Length(
Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(
to_char(EMPNO),
'1','*'),
'2','**'),
'3','***'),
'4','****'),
'5','*****'),
'6','******'),
'7','*******'),
'8','********'),
'9','*********'),
'0')),0) as SUM_ENO,
SAL,
nvl(Length(
Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(
to_char(SAL),
'1','*'),
'2','**'),
'3','***'),
'4','****'),
'5','*****'),
'6','******'),
'7','*******'),
'8','********'),
'9','*********'),
'0')),0) as SUM_SAL
from WorkTable;
--■■■model句を使う方法(10g以降)■■■
select EMPNO,SUM_ENO,SAL,SUM_SAL
from WorkTable
model
dimension by (RowNum as soeji)
measures(EMPNO,EMPNO as wk_EMPNO,0 as SUM_ENO,SAL,SAL as wk_SAL,0 as SUM_SAL)
rules ITERATE(20)(
SUM_ENO[any] = SUM_ENO[cv()] + mod(wk_EMPNO[cv()],10),
wk_EMPNO[any] = trunc(wk_EMPNO[cv()]/10),
SUM_SAL[any] = SUM_SAL[cv()] + mod(wk_SAL[cv()],10),
wk_SAL[any] = trunc(wk_SAL[cv()]/10));