简体   繁体   English

如何计算计算列的平均值

[英]How to do an average of computed column

I have data in a table called transaction, I have derived purchase value ie final_rat1 * quantity and sum of purchase quantity 我在名为交易的表中有数据,我已经得出购买价值,即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

Below is the code for it, but when I am averaging purchase value / sum of purchase quantity I'm unable to get proper average. 下面是它的代码,但是当我平均购买价值/购买数量总和时,我无法获得正确的平均值。 Could you please guide me on this. 请您指导我。 Below is the code, which I have mentioned 下面是我提到的代码

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"

Actual Average should be 88.14 where as im getting an average of 616.98 and if i remove the sum from below average query 实际平均值应为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"

it says "not a group by function" 它说“不是按功能分组”

Perhaps you mean something like this: 也许您的意思是这样的:

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM