如何做计算列的平均值

时间:2013-12-04 06:37:31

标签: sql plsql oracle10g

我在一个叫做交易的表中有数据,我已经得出了购买价值,即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"

它说“不是按功能分组”

1 个答案:

答案 0 :(得分:0)

也许你的意思是这样的:

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"