比如我一个字段的计算公式是这样的(3000000035-3000000033)/(9109001544+9109001550+9109001545+9109001551)* 9109441531。3000000035和表里面字段的对应关系记录在表table里面的,现在想要生成这样的sql语句
case when (sum(c)+sum(d)+sum(e)+sum(f)=0 then 0 else (sum(a)-sum(b))/(sum(c)+sum(d)+sum(e)+sum(f))*sum(g) end,用pl/sql要怎么去实现?
------解决思路----------------------
SQL> with T1 AS (
2 SELECT 3000000035 ID,'a' NAME FROM DUAL UNION ALL
3 SELECT 3000000033 ID,'b' NAME FROM DUAL UNION ALL
4 SELECT 9109001544 ID,'c' NAME FROM DUAL UNION ALL
5 SELECT 9109001550 ID,'d' NAME FROM DUAL UNION ALL
6 SELECT 9109001545 ID,'e' NAME FROM DUAL UNION ALL
7 SELECT 9109001551 ID,'f' NAME FROM DUAL UNION ALL
8 SELECT 9109441531 ID,'g' NAME FROM DUAL
9 ),T AS (
10 select '(3000000035-3000000033)/(9109001544+9109001550+9109001545+9109001551)* 9109441531' str from dual
11 ),T2 AS (
12 SELECT 'SUM('
------解决思路----------------------
T1.NAME
------解决思路----------------------
')' STR,A.L FROM(
13 SELECT REGEXP_SUBSTR(STR,'[0-9]+',1,LEVEL) STR,REGEXP_INSTR(STR,'[0-9]+',1,LEVEL) L FROM T
14 CONNECT BY LEVEL<=REGEXP_COUNT(STR,'[0-9]+')
15 )A,T1 WHERE A.STR=T1.ID
16 UNION ALL
17 SELECT REGEXP_SUBSTR(STR,'[^0-9]+',1,LEVEL) STR,REGEXP_INSTR(STR,'[^0-9]+',1,LEVEL) L FROM T
18 CONNECT BY LEVEL<=REGEXP_COUNT(STR,'[0-9]+')
19 )
20 SELECT LISTAGG(STR,'')WITHIN GROUP(ORDER BY L)
21 FROM T2;
LISTAGG(STR,'')WITHINGROUP(ORDERBYL)
--------------------------------------------------------------------------------
(SUM(a)-SUM(b))/(SUM(c)+SUM(d)+SUM(e)+SUM(f))* SUM(g)