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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:27:52+00:00 2026-05-11T11:27:52+00:00

SELECT user_id, SUM(COALESCE(point_points, 0)) AS total_points, SUM( CASE WHEN point_date > ‘$this_month’ THEN point_points

  • 0
SELECT user_id,     SUM(COALESCE(point_points, 0)) AS total_points,     SUM(         CASE             WHEN point_date > '$this_month'             THEN point_points             ELSE 0         END)                AS month_points,     COUNT(DISTINCT c_id)    AS num_comments,     COUNT(DISTINCT rant_id) AS live_submissions FROM users     LEFT JOIN points     ON  users.user_id = points.point_userid     LEFT JOIN comments     ON         (             c_userid = user_id         )     LEFT JOIN rants     ON         (             rant_poster = user_id         AND rant_status = 1         ) WHERE user_id = $id GROUP BY user_id 

Basically live_submissions and num_comments variable display proper results, while the total_points and month_points display a product of month_points/total_points, live_submissions and num_comments. Any idea why this is happening?

  • 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. 2026-05-11T11:27:53+00:00Added an answer on May 11, 2026 at 11:27 am

    This is called a Cartesian Product. When you join the tables together, the default result is every permutation of rows for which the join conditions are true. You use JOIN conditions to limit these permutations.

    But since you are joining multiple tables to users, the result includes every permutation of each matching table. For example, each matching row in points is repeated per matching row in comments, and each of these is multiplied again, repeating per matching row in rants.

    You can partially compensate for this with COUNT(DISTINCT c_id) as you are doing, but the DISTINCT is necessary only because you have multiple rows per c_id. And it doesn’t work unless you apply it to unique values. This remedy doesn’t work for the SUM() expressions.

    Basically, you’re trying to do too many calculations in one query. You need to split it up into separate queries for it to be reliable. And then you can get rid of the DISTINCT modifiers, too.

    SELECT u.user_id, SUM(COALESCE(p.point_points, 0)) AS total_points,    SUM( CASE WHEN p.point_date > '$this_month' THEN p.point_points ELSE 0 END ) AS month_points FROM users u LEFT JOIN points p   ON u.user_id = p.point_userid  WHERE u.user_id = $id GROUP BY u.user_id;  SELECT user_id, COUNT(c.c_id) as num_comments,  FROM users u LEFT JOIN comments c   ON (c.c_userid = u.user_id) WHERE u.user_id = $id GROUP BY u.user_id;  SELECT u.user_id, COUNT(r.rant_id) as live_submissions FROM users u LEFT JOIN rants r   ON (r.rant_poster = u.user_id AND r.rant_status = 1) WHERE u.user_id = $id GROUP BY u.user_id; 

    You shouldn’t try to do all three of these in a single query.

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

Sidebar

Ask A Question

Stats

  • Questions 106k
  • Answers 106k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer How about this? Expression<Func<int, int>> funcExpr2 = (pArg) => foo.AFuncIntInt(pArg);… May 11, 2026 at 8:50 pm
  • Editorial Team
    Editorial Team added an answer I wrote a blog post about doing this with jQuery… May 11, 2026 at 8:50 pm
  • Editorial Team
    Editorial Team added an answer Check out jQuery UI Tabs; this will do what you're… May 11, 2026 at 8:50 pm

Related Questions

SELECT user_id, SUM(COALESCE(point_points, 0)) AS total_points, SUM( CASE WHEN point_date > '$this_month' THEN point_points
Okay, my dilemma is this. I have an admin page that I use to
Im trying to get the total amount of points a user has, as well
I have this database structure: TBL_A | TBL_B | TBL_C | TBL_D | TBL_E
I need to query this DB to get each row, but also the SUM

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.