Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9092919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:57:32+00:00 2026-06-16T22:57:32+00:00

I have been working at this one for an hour. I have tried several

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T22:57:33+00:00Added an answer on June 16, 2026 at 10:57 pm

    Try this:

    SELECT
        poster_id,
        voter_id,
        count(*) AS votes,
        count(*) * 100 / total.total_votes as percentage
    FROM table_1
    join (
        select poster_id, count(*) AS total_votes
        FROM table_1
        join table_2 on table_1.post_id = table_2.post_id
        GROUP BY poster_id
    ) total on total.poster_id = table_1.poster_id
    join table_2 on table_1.post_id = table_2.post_id
    GROUP BY poster_id, voter_id
    ORDER BY votes DESC
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been working on this for one hour, just can't get it. I have
This is one of the problem i have been working from a long time.This
Okay this one has me stumped.. mainly because i have been working on this
I have been working this one for a few hours now trying get this
have been working and wearing out my fingers doing google searches with this one:
Have been working on this question for a couple hours and have come close
I have been working on this app for at least 3-4 months and just
I have been working on this sort of ATM (With a maximum of 50
I have been working on this for few hours and got stuck, so how
I have been working on this application that enables user to log in into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.