【问题标题】:How to do an average of computed column如何对计算列进行平均
【发布时间】:2013-12-04 06:37:31
【问题描述】:

我在一个名为 transaction 的表中有数据,我得出了购买价值,即 final_rat1 * quantity 和购买数量的总和

Buysell     Quantity    Final_rat1 
B           50          88.14
B           230         88.14
B           75          88.14
B           87          88.14
B           187         88.14
B           150         88.14
B           221         88.14

下面是它的代码,但是当我平均购买价值/购买数量的总和时,我无法获得正确的平均值。你能指导我吗?下面是代码,我已经提到了

sum(case
        when sauda.buysell = 'B' then
            sauda.quantity * 1
        else
            0
        end) as "P.Qty",
sum(case
        when sauda.buysell = 'B' then
            sauda.quantity * (sauda.final_rat1 + sauda.brokpercontract)
        else
            0
        end) As "P.value",
sum(case
        when sauda.buysell = 'B' then
            sauda.quantity * (sauda.final_rat1 + sauda.brokpercontract)/sauda.quantity
        else
            0
        end) as "P.Avg"

实际平均值应为 88.14,其中我得到的平均值为 616.98,如果我从低于平均水平的查询中删除总和

case
    when sauda.buysell = 'B' then
        sauda.quantity * (sauda.final_rat1 + sauda.brokpercontract)/sauda.quantity
    else
        0
    end as "P.Avg"

它说“不是按功能分组”

【问题讨论】:

  • 更新问题,在已发布的表数据中添加brokpercontract 列..
  • sauda.quantity * (sauda.final_rat1 + sauda.brokpercontract)/sauda.quantity 只会给你 sauda.final_rat1 + sauda.brokpercontract 对吗?让你的问题更清楚,这个专栏 brokpercontract 是什么?也添加。

标签: sql plsql oracle10g


【解决方案1】:

也许你的意思是这样的:

sum(case
    when sauda.buysell = 'B' then
        sauda.quantity * 1
    else
        0
    end) as "P.Qty",
sum(case
    when sauda.buysell = 'B' then
        sauda.quantity * (sauda.final_rat1 + sauda.brokpercontract)
    else
        0
    end) As "P.value",
avg(case
    when sauda.buysell = 'B' then
        sauda.final_rat1 + sauda.brokpercontract
    else
        0
    end) as "P.Avg"

【讨论】:

    猜你喜欢
    • 2017-11-26
    • 2011-03-08
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    相关资源
    最近更新 更多