简体   繁体   English

如何计算 SQL 中的组总和?

[英]How to calculate sum of group in SQL?

I'm trying to find how to calculate a new SQL column that works on a formula involving 'NA' values.我试图找到如何计算一个新的 SQL 列,该列适用于涉及“NA”值的公式。 I think that I am not getting my desired result because of either the 'NA' values or because of my grouping.我认为由于“NA”值或由于我的分组,我没有得到我想要的结果。 Please see the table and formula below:请看下表和公式:

My table is the following:我的表如下:

website session score
 google    1       1
 google    2       NA
 bbc       3       2
 bbc       4       4
 bbc       5       5

The formula i am using to created a new calculated field is the following:我用来创建新计算字段的公式如下:

select
    website,
    (sum(score IN (4,5))/sum((score is not null)) - sum(score IN (1,2))/sum(score is not null))*100 as new_column
from 
    my_table
group by 
    website

Essentially the formula is trying to do the following:本质上,该公式试图执行以下操作:

Sum up those values in the score column that are either 4 or 5, then divide them by the sum of the numbers in the score column that do exist.将分数列中 4 或 5 的值相加,然后将它们除以分数列中确实存在的数字的总和。

Then deduct the following:然后扣除以下内容:

Sum up those values in the score column that are either 1 or 2, then divide them by the sum of the numbers in the score column that do exist.将分数列中 1 或 2 的值相加,然后将它们除以分数列中确实存在的数字的总和。

Finally:最后:

Multiply the entire output by 100将整个 output 乘以 100

The formula has to be applied to each group.该公式必须应用于每个组。

So you should have the following desired result:所以你应该有以下期望的结果:

Taking only the first group of "google"只取第一组“google”

You have 4 not null values.您有 4 个非 null 值。 you have 2 values whereby the value is IN (1,2) and 0 values in (4,5).您有 2 个值,其中值为 IN (1,2),0 值为 (4,5)。

so you would have the following formula:所以你会有以下公式:

((0/4)-(2/4))*100 ((0/4)-(2/4))*100

-2/4 * 100 -2/4 * 100

= -50 = -50

however, when calculating this in SQL i recieve the value -100.但是,在 SQL 中计算时,我收到的值是 -100。 which does not make sense to me.这对我来说没有意义。 can anyone help?谁能帮忙?

You have one not null values for Google.你有一个不是谷歌的 null 值。 The sum of it is one.它的总和是一。 So your formula is resulting in (0-1/1)*100 which is -100, which is what it is giving.所以你的公式导致 (0-1/1)*100 是-100,这就是它给出的结果。

What I think you want is the count of all Google records divided by the count of all non-null records?我认为您想要的是所有 Google 记录的计数除以所有非空记录的计数?

I advise multiplying by 1.0 each sum so that it doesn't round the sum to int value which would result in zero(0) for 'bbc' group.我建议将每个总和乘以 1.0,这样它就不会将总和四舍五入为 int 值,这将导致“bbc”组为零(0)。

Here is the updated version of your query(Check also SQL fiddle http://sqlfiddle.com/#!5/5fca1/8 ):这是您查询的更新版本(也检查 SQL 小提琴http://sqlfiddle.com/#!5/5fca1/8 ):

select website,
    (sum(score IN (4,5)) * 1.0/sum(score is not null) - sum(score IN (1,2)) * 1.0/sum(score is not null))*100.0 as new_column
from my_table
group by website

I think you are looking for a difference between percentage of (4,5) and (1,2) for each group.我认为您正在寻找每组的(4,5)和(1,2)百分比之间的差异。 Since null values are ignored in aggregation, you could simply do由于 null 值在聚合中被忽略,你可以简单地做

(avg(score in (4,5)) - avg(score in (1,2)))*100

From your manual example, You may first sum all the not null values, and then use that value in your code -从您的手动示例中,您可以先将所有非 null 值相加,然后在代码中使用该值 -

WITH NN_SUM AS (SELECT SUM(score is not null) NOT_NULL_SUM
                FROM my_table)
SELECT website,
       (((sum(score IN (4,5)) - sum(score IN (1,2))) / NOT_NULL_SUM) * 100 as new_column
FROM my_table
CROSS JOIN NN_SUM
GROUP BY website;

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

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