--Create table create table TESTRRT ( complexid NUMBER not null,--复合指标id basicid VARCHAR2(20) not null,--基础指标id chu VARCHAR2(20),--除法标示(a为分子,b为分母) plus VARCHAR2(20),--乘法标示 direction NUMBER,--计算方向(1为正2为负) value NUMBER )
复合指标1:(1001+1003)/(1002-1004)=-12
复合指标2:1006*1007/(2011+2022)=1.5
复合指标3:1009*1010/1011=0.5
--单独除法关系的指标 select t.complexid, decode(sum(case when t.chu = 'b' and t.direction = 1 then t.VALUE when t.chu = 'b' and t.direction = 2 then -t.VALUE end), 0, 0, sum(case when t.chu = 'a' and t.direction = 1 then t.VALUE when t.chu = 'a' and t.direction = 2 then -t.VALUE end) / sum(case when t.chu = 'b' and t.direction = 1 then t.VALUE when t.chu = 'b' and t.direction = 2 then -t.VALUE end)) from testrrt t where t.complexid = 1 group by t.complexid
网上查找相关资料表明, http://blog.sina.com.cn/s/blog_63f3c0b201015gh9.htmloracle的累乘用法已经被很多人使用过啦。
exp(y)
【功能】返回e的y次幂(e为数学常量)
【参数】y,数字型表达式
ln(y)
【功能】返回e为底的自然对数。
由于ln对应的y参数仅可为正数,所以如下进行连乘的方法只能针对正数,不包含负数哦~
--先乘后除指标【累乘算法:exp(ln(a)+ln(b)+ln(c))=a*b*c】 select t.complexid, decode(exp(sum(ln(case when t.plus = 'p' and t.chu = 'b' then t.value end))),0,0,exp(sum(ln(case when t.plus = 'p' and t.chu = 'a' then t.value end)))/sum(case when t.plus is null and t.chu = 'b' then t.value end) ) mvalues from testrrt t where t.complexid in (2,3) group by t.complexid