简体   繁体   中英

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. A single vote is stored as a -1 or a 1.

Now I get the following and want to know what the number of positive votes is:

  • SUM: -2
  • Total votes: 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)

-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.

Or in more strong mathematical terms:

更强大的数学

接着

Why not just store negative as 0?

Positive would then be sum and negative would be total - sum.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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