简体   繁体   English

从总投票数和所有投票结果中获得正面投票数(+/-)

[英]Get number of positive votes from total of votes and result of all votes (+/-)

I have a query which returns the total number of votes (COUNT()) and the sum (SUM()) of all votes. 我有一个查询,该查询返回总票数(COUNT())和所有票数的总和(SUM())。 A single vote is stored as a -1 or a 1. 单个投票存储为-1或1。

Now I get the following and want to know what the number of positive votes is: 现在,我得到以下内容,想知道正面投票的数目是多少:

  • SUM: -2 总计:-2
  • Total votes: 4 总票数:4

What is the formula to get the number of positive (and negative) votes? 获得正面(和负面)投票数的公式是什么?

In the example the outcome is: -1 + -1 + -1 + 1 (3 negative votes and 1 positive) 在该示例中,结果为:-1 + -1 + -1 + 1(3个否定票和1个肯定票)

-x +y = sum
x + y = tot
=> 2y = sum + tot
=> y = (sum + tot)/2 ; x = tot - y

where y is the number of positive votes and x the negatives. 其中y是正面投票数, x是负面投票数。

Or in more strong mathematical terms: 或更严格的数学术语:

更强大的数学

接着

Why not just store negative as 0? 为什么不将负数存储为0?

Positive would then be sum and negative would be total - sum. 正数将是总和,负数将是总和。

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

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