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

  • Home
  • SEARCH
  • 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 8193765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:35:41+00:00 2026-06-07T04:35:41+00:00

I have the following SQL query. SELECT u.username, SUM(p.points) AS points, SUM(sp.spPoints) AS spPoints,

  • 0

I have the following SQL query.

SELECT    u.username, SUM(p.points) AS points,
          SUM(sp.spPoints) AS spPoints,
          (SUM(sp.spPoints) - SUM(p.points)) AS Puntos_Restantes
FROM      users as u
LEFT JOIN points as p ON (u.userid = p.userid)
LEFT JOIN sppoints AS sp ON (u.userid = sp.userid)
WHERE     u.userid = '1'
GROUP BY  u.userid

My goal is to SUM 2 fields and then subtract them but when I execute the above query, the second SUM is wrong.
The tables are like this:

points: pointId, userId, points    
sppoints: spPointId, userId, spPoints    

In points I have this amount: 25 and in spPoints: 10
but when I run the query I get :

points  spPoints    Puntos_Restantes
 25       30              5

What is going wrong here?

  • 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-07T04:35:43+00:00Added an answer on June 7, 2026 at 4:35 am

    The table users has a one-to-many relationship to both the other 2 tables. This causes the 2 joins to produce a mini-Carstesian product and multiple rows with same data in the points columns – which are then aggregated.

    You can use subqueries to group by, and then join, to avoid this problem:

    SELECT 
          u.username
        , COALESCE(p.pPoints,0) 
            AS pPoints
        , COALESCE(sp.spPoints,0) 
            AS spPoints
        , COALESCE(p.pPoints,0) - COALESCE(sp.spPoints,0)
            AS Puntos_Restantes
    FROM 
          users as u
      LEFT JOIN 
          ( SELECT userid, SUM(points) AS pPoints
            FROM points
            WHERE userid = 1
            GROUP BY userid
          ) AS p
        ON u.userid = p.userid
      LEFT JOIN 
          ( SELECT userid, SUM(spPoints) AS spPoints
            FROM sppoints
            WHERE userid = 1
            GROUP BY userid
          ) AS sp
        ON u.userid = sp.userid
    WHERE u.userid = 1 ;
    

    If you want to have results for more than one user (or for all users), replace the three WHERE userid=1 conditions (or remove them altogether).

    Indices on points(userid, points) and sppoints(userid, spPoints) will help with efficiency.

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

Sidebar

Related Questions

I have following SQL query: SELECT Count(*) AS CountOfRecs FROM tblAccount INNER JOIN tblAccountOwner
I have following SQL query SELECT TOP 10000 AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability FROM Nodes INNER
I have the following SQL Server query: SELECT area FROM places WHERE REPLACE(area,'-',' ')
I have the following which works in SQL Query Analyzer . select oh.* from
I have the following SQL Query: SELECT upd.*, usr.username AS `username`, usr.profile_picture AS `profile_picture`
I have following LINQ to SQL query expression from msg in TblUserMessages join user
I have the following SQL query: select expr1, operator, expr2, count(*) as c from
I have the following SQL statement: mysql_query(SELECT * FROM `friends` WHERE friend1='$username' OR friend2='$username'
I have the following SQL query: SELECT DISTINCT ProductNumber, PageNumber FROM table I am
I have the following SQL query: SELECT r.BEZEICHNUNG AS BEZEICHNUNG, r.ID AS ID, ra.BEZEICHNUNG

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.