我创建了一个带有最新更改金额的查询,但我还想包括新金额与前一金额是"Increase"
还是"Decrease"
以及%
介于之前和当前金额之间。
这是我的疑问:
SELECT
RATE.FK_ORGA_FRAN,
RATE.FK_ORGA_OPER,
RATE.FED,
RATE.FK_RCOM,
RATE.FK_PROD,
RATE.FK_CDIR,
RATE.FK_TIER,
RATE.FK_TPRE,
RATE.FK_CURR,
RATE_STEP.UNIT_COST,
RATE.FK_CURR
from RATE , RATE_STEP
WHERE
RATE.FK_ORGA_FRAN = RATE_STEP.FK_ORGA_FRAN AND
RATE.FK_ORGA_OPER = RATE_STEP.FK_ORGA_OPER AND
RATE.FK_RCOM = RATE_STEP.FK_RCOM AND
RATE.FK_PROD = RATE_STEP.FK_PROD AND
RATE.FK_CDIR = RATE_STEP.FK_CDIR AND
RATE.FK_TIER = RATE_STEP.FK_TIER AND
RATE.FK_TPRE = RATE_STEP.FK_TPRE AND
RATE.FK_PMAT = RATE_STEP.FK_PMAT AND
RATE.FED = RATE_STEP.FK_RATE_FED AND
RATE.FED BETWEEN (SYSDATE) -16 AND (SYSDATE)
你能帮帮忙吗?
非常感谢 佛瑞德
答案 0 :(得分:0)
假设您使用的是oracle SQL,
将样本数据作为:
DT Rate
14-JAN-14 19
15-JAN-14 11
17-JAN-14 14
18-JAN-14 15
19-JAN-14 11
21-JAN-14 12
23-JAN-14 15
create table rate_log (dt date, rate number(5));
使用以下查询并对其应用类似的逻辑。
WITH data AS
( SELECT rownum rn, t.* FROM
( SELECT dt, rate FROM rate_log ORDER BY dt
)t
)
SELECT d1.dt,
d1.rate,
ROUND(( d1.rate - d2.rate ) * 100 / d1.rate, 2 )diff_perc,
CASE
WHEN(( d1.rate - d2.rate ) < 0 )
THEN 'Increase'
ELSE 'Decrease'
END change
FROM data d1,
data d2
WHERE( d1.rn - 1 ) =( d2.rn(+) )
ORDER BY d1.dt;