Note that there are some explanatory texts on larger screens.

plurals
  1. POJoining votes between tables and getting a percentage
    primarykey
    data
    text
    <p>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.</p> <p>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).</p> <p>Table 1 has columns: post_id, poster_id Table 2 has columns: post_id, voter_id, vote</p> <p>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.</p> <pre><code>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 </code></pre> <p>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:</p> <pre><code>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% </code></pre> <p>etc.</p> <p>Basically voter #3 is responsible for 100% of poster #1's votes. Poster #3 got 60% of its votes from voter #1... etc.</p> <p>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.</p> <p>I thought a RIGHT JOIN would work, but it is not working out.</p> <pre><code>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 </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload