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 3273244
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:54:42+00:00 2026-05-17T18:54:42+00:00

I currently have two tables, one with documents, and another with ratings doc_id |

  • 0

I currently have two tables, one with documents, and another with ratings

doc_id | doc_groupid | doc_name | doc_time

and then

rating_id | rating_docid | rating_score

where rating_score is either -1 or 1.

What I need to do is have a single query that retrieves every column in the document table WHERE groupid = #, but also has columns which aggregate the ratings. I can retrieve a list of ratings using

  SELECT rating_docid,
         SUM(CASE WHEN rating_type = 1 THEN 1 ELSE 0 END ) AS UpVotes,
         SUM(CASE WHEN rating_type = -1 THEN 1 ELSE 0 END) AS DownVotes
GROUP BY rating_docid

Which gives me a list of documents (as long as they have been rated) and how many upvotes or downvotes they have. I can also obviously very easily get a list of documents with

SELECT * FROM documents WHERE doc_groupid = #

But I have no idea how to do this without a subquery (using JOIN or LEFT JOIN), which my understanding is too slow. Honestly, I have no idea how to do this with a subquery either.

So my question is:

  1. How can I do this with a speedy join?
  2. How can I do this with a subquery?

Thanks!

  • 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-05-17T18:54:42+00:00Added an answer on May 17, 2026 at 6:54 pm

    Use:

       SELECT d.doc_id,
              d.doc_name,
              d.doc_time, 
              COALESCE(SUM(CASE WHEN r.rating_type = 1 THEN 1 ELSE 0 END), 0) AS upvotes,
              COALESCE(SUM(CASE WHEN r.rating_type = -1 THEN 1 ELSE 0 END), 0) AS downvotes
         FROM DOCUMENTS d
    LEFT JOIN RATINGS r ON r.rating_docid = d.doc_id
        WHERE d.doc_groupid = ?
     GROUP BY d.doc_id, d.doc_name, d.doc_time
    

    The doc_time is odd to me, makes me think you can have duplicates but with different time values…

    JOIN vs Subquery

    JOINs (INNER and OUTER) are not subqueries. To make things more complicated, subqueries can mean:

    • a query in the SELECT clause (AKA sub-select):

      SELECT (SELECT col FROM TABLE) AS col2, ...
      
    • a query in the WHERE or HAVING clauses:

      WHERE col = (SELECT column FROM TABLE)
      HAVING col IN (SELECT cols FROM TABLE)
      
    • a query in the JOIN (AKA derived table, inline view):

      LEFT JOIN (SELECT u.user,
                        COUNT(*) AS num
                   FROM TABLE u
               GROUP BY u.user) x ON x.user = t.column
      

    There’s no hard’n’fast rule about one being better than the other because it all depends on:

    • table structure
    • data
    • indexing and table statistics
    • expected results

    All that really matters is the work is done in as few passes over a table as necessary–ideally one.

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

Sidebar

Related Questions

Currently I have two MediaWikis, one a slightly older version than the other. I
I currently have two text boxes which accept any number. I have a text
All, I currently have two projects that are under SourceSafe that I am unable
I currently have an asp.net website hosted on two web servers that sit behind
I have an Auction model with several attributes, two of which are current_auction:boolean and
I currently have an MS Access application that connects to a PostgreSQL database via
I currently have speakers set up both in my office and in my living
I currently have an existing database and I am using the LINQtoSQL generator tool
We currently have a company email server with Exchange, and a bulk email processing
I currently have a fairly robust server-side validation system in place, but I'm looking

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.