Question

I have three tables in my RDBMS (most often MySQL or Postgresql) which I want to use when calculating votes. The first is a user table where users have different levels of pull (weight), the second is a table which records their vote on an item, and the third is the item table itself.

The idea is that moderators, or users that have been around longer should have more "weight" when casting votes.

USER {
    id,
    weight int,
}

VOTE {
    vote int,
    user_id,
    item_id,
}

ITEM {
    id
}

I want to calculate votes for items based on the weight of the user voting in addition to the number of votes. So if user 100 and 101 both have a weight of 1 and vote a score of "3", and user 102 with a weight of 10 comes along and votes "5" then we assign more value to vote cast by this user 102.

Perhaps something simple like the total user weight of all users combined being used to divide the votes.

(user:weight * vote) / sum(user:weight))

Perhaps I should use some other formula to curve the power of the users with the most weight in the system.

How can I calculate votes taking a third variable like the power of the user into account?

For that mater, how should I calculated weight? Perhaps I could add the combined weight of all users up, then divide by the total number of users to find an average weight I can used to give the user a weight of between 1 and 100 so it's easier to work with.

Was it helpful?

Solution

You can just do the arithmetic in an aggregation:

select v.item_id,
       (case when sum(u.weight) > 0 then sum(u.weight * v.vote) / sum(u.weight) end) as weightedVote
from vote v join
     user u
     on v.user_id = u.id
group by v.item_id

How you calculate the weights is a different matter. That query shows how to use them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top