我有一个包含以下列的测试表:
Transaction_date date
account_number Number(10)
transaction_ammount number (10,5)
account_number transaction_amount transaction date
111 10000 10-OCT-2014
111 20000 10-OCT-2014
111 50000 08-OCT-2014
111 30000 06-OCT-2014
222 60000 10-OCT-2014
222 50000 10-OCT-2014
222 30000 08-OCT-2014
我需要根据以下公式计算评分:
评级=((1天的总交易额) - (最近10个日历天的平均总值))/ 最后10天的标准偏差(STDDEV)。
我已经使用STDDEV函数来计算它。但它给我的输出不正确。
account_number 111的要求是:
排名第10 OCT =第10个OCt-Avg的所有金额为111(过去10天)/ STDEV为帐户111。
排名第10 OCT =(10000 + 20000) - ((10000 + 2000 + 50000 + 30000)/ 10)/ STDEV(10000,2000,50000,30000)
请建议我如何在sql query的帮助下实现这一目标。谢谢!
答案 0 :(得分:0)
with t1 as (
select '111' account_number, 10000 transaction_amount, to_date('10-10-2014', 'DD-MM-YYYY') transaction_date from dual
union all select '111', 20000, to_date('10-10-2014', 'DD-MM-YYYY') from dual
union all select '111', 50000, to_date('08-10-2014', 'DD-MM-YYYY') from dual
union all select '111', 30000, to_date('06-10-2014', 'DD-MM-YYYY') from dual
union all select '222', 60000, to_date('10-10-2014', 'DD-MM-YYYY') from dual
union all select '222', 50000, to_date('10-10-2014', 'DD-MM-YYYY') from dual
union all select '222', 30000, to_date('08-10-2014', 'DD-MM-YYYY') from dual
),
t2 as (
select account_number, transaction_amount, transaction_date,
-- aggregate transaction_amount for 1 day
sum(transaction_amount) over (partition by account_number order by transaction_date range 0 preceding) aggr_amount,
-- Avg aggregate value for last 10 calender days
avg(transaction_amount) over (partition by account_number order by transaction_date range 10 preceding) aggr_avg,
-- Standared Deviation(STDDEV) for last 10 days
stddev(transaction_amount) over (partition by account_number order by transaction_date range 10 preceding) aggr_stddev
from t1)
select account_number, transaction_amount, transaction_date,
aggr_amount - aggr_avg / nullif(aggr_stddev, 0)
from t2;