I have been working at this one for an hour. I have tried several different joins, and subqueries with no luck. Here is the situation.
Two tables. One with an main index, and one a listing of votes from users. I want to determine how many votes a particular user is leaving for another user (easy)… THEN figuring out the percentage of the total votes and sorting by that (hard).
Table 1 has columns: post_id, poster_id
Table 2 has columns: post_id, voter_id, vote
The post_id is correlated between the two tables. A simple query like this will get you an output showing the total votes 1 user has left another user… then sorts showing who has left the most votes for another.
SELECT poster_id, voter_id, count(*) AS votes
FROM table_1, table_2
WHERE table_1.post_id = table_2.post_id
GROUP BY poster_id, voter_id
ORDER BY votes DESC
That works great… but I want to see who is leaving the most votes as a percentage of the users total votes. So I need to ALSO get the total votes a “poster_id” has, then divide the current number into a percentage… then sort into that percentage. Output should be something like:
poster_id | voter_id | votes | vote_total | percent
----------------------------------------------------
1 | 3 | 10 | 10 | 100%
3 | 1 | 15 | 25 | 60%
2 | 1 | 3 | 6 | 50%
2 | 3 | 2 | 6 | 33%
3 | 2 | 5 | 25 | 20%
2 | 4 | 1 | 6 | 17%
etc.
Basically voter #3 is responsible for 100% of poster #1’s votes. Poster #3 got 60% of its votes from voter #1… etc.
We’re trying to find out if there is a particular user giving someone more votes (as a percentage) than other users to try and find potential abuses.
I thought a RIGHT JOIN would work, but it is not working out.
SELECT t1.poster_id, t1.voter_id, count(*) AS votes, count(t3.*) AS votes_total, votes / votes_total AS percentage
FROM (table_1 t1, table_2 t2)
RIGHT JOIN (table_1 t3, table_2 t4)
ON (t3.post_id = t4.post_id AND t3.poster_id = t1.poster_id)
WHERE t1.post_id = t2.post_id
GROUP BY t1.poster_id, t2.voter_id
ORDER BY percentage DESC
Basically runs forever and doesn’t return anything. I typed that query from memory, and doesn’t exactly represent the real table names. Any points in the right direction would help. Inner join perhaps?
Try this:
This uses a subquery to return the total votes for each poster_id then joins to it as if it were a table.
Note also the change to using proper joins instead of joining through the where clause.