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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:04:00+00:00 2026-06-10T17:04:00+00:00

I have the following two tables Table: items ID | TITLE 249 | One

  • 0

I have the following two tables

Table: items
ID  |  TITLE
249 | One
250 | Two
251 | Three

And I have voting for these:

Table: votes
VID | IID | userid | votes
01  | 249 |    6   | 5
02  | 249 |    7   | -5
03  | 249 |    8   | 5
04  | 249 |    9   | 5
05  | 250 |    6   | -5
06  | 250 |    7   | -5
07  | 250 |    8   | 5

-5 means a DOWNVOTE, and +5 means an upvote. Assuming I am logged in as user 6, what SQL query will give me:

Table: result
ID  |  TITLE | TOTALVOTES | UPVOTES | DOWNVOTES | CURRENTUSERVOTED
249 | One    | 4          | 3       | 1         | 1
250 | Two    | 3          | 1       | 2         | 1
251 | Three  | 0          | 0       | 0         | 0
  • 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-10T17:04:02+00:00Added an answer on June 10, 2026 at 5:04 pm

    Use CASE expressions within your aggregate functions:

    SELECT    a.ID,
              a.TITLE,
              COUNT(b.IID) AS TOTALVOTES,
              COUNT(CASE WHEN b.votes =  5 THEN 1 END) AS UPVOTES,
              COUNT(CASE WHEN b.votes = -5 THEN 1 END) AS DOWNVOTES,
              COUNT(CASE WHEN b.userid = 6 THEN 1 END) AS CURRENTUSERVOTED
    FROM      items a
    LEFT JOIN votes b ON a.ID = b.IID
    GROUP BY  a.ID,
              a.TITLE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables in my database as following: Suggestions Table: ID, Title, Description,
I have the following two tables in my mysql database. Table Name: groups id
I have the following two tables, affiliates and referrers. affiliates Table id loginid 3
I have two tables with the following (simplified) structures: table "Factors" which holds data
So I have two tables. Table 1 is named 'appointment' with the following fields:
I have this following problem: I have two tables 1 table for customer bought
I have the following two tables (SQL scripts with data): CREATE TABLE [dbo].[Item_Master]( [Item_Name]
I've got one table containing some sort of items , and two tables (
I have two tables: CREATE TABLE items ( root_id integer NOT NULL, id serial
I have two tables with a many-to-one relationship. (Oracle) **Table: PARENT** Field: A (PK)

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.