【问题标题】:Calculate Standard Deviation in Oracle在 Oracle 中计算标准差
【发布时间】:2014-10-18 04:12:33
【问题描述】:

我有一个包含以下列的测试表:

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

我需要根据以下公式计算评分:

rating=((1 天的总交易量)-(过去 10 个日历日的平均总价值))/ 过去 10 天的标准偏差 (STDDEV)。

我使用了 STDDEV 函数来计算它。但是它给了我不正确的输出。

account_number 111 需要的是:

第 10 个 OCT 的排名=111(过去 10 天)的所有金额的第 10 个 OCt-Avg 的 SUM/帐户 111 的 STDEV。

10 月 10 日的排名=(10000+20000)-((10000+2000+50000+30000)/10)/STDEV(10000,2000,50000,30000)

请建议我如何在 sql 查询的帮助下实现这一点。谢谢!

【问题讨论】:

  • stddev() 的输出究竟以何种方式不正确?你知道你的错别字吗? (测试表中有 20000 个,但 10 月 10 日在您的排名中的两个不同位置只有 2000 个。)
  • 我必须同意@MikeSherrill'CatRecall',如果我们看不到您正在执行的代码并且考虑到您的逻辑存在缺陷,我不得不假设您做错了。

标签: oracle


【解决方案1】:
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;       

【讨论】:

  • 感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2017-09-26
  • 2016-04-14
  • 2020-01-28
  • 2012-08-12
  • 2022-01-21
  • 2013-02-07
  • 2015-11-20
  • 2020-08-13
相关资源
最近更新 更多