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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:07:38+00:00 2026-06-15T21:07:38+00:00

I basically have a table that holds counts for every date. I want to

  • 0

I basically have a table that holds counts for every date. I want to create a query that gives me the total # of counts over the entire table, as well as the total for yesterday. But when I try to join the table twice, the aggregates are off. Below is how you can replicate the results.

CREATE TABLE a (id int primary key);
CREATE TABLE b (a_id int, b_id int, date date, count int, primary key (a_id,b_id,date));
INSERT INTO a VALUES (1);
INSERT INTO b VALUES (1, 1, UTC_DATE(), 5);
INSERT INTO b VALUES (1, 2, UTC_DATE(), 10);
INSERT INTO b VALUES (1, 1, UTC_DATE()-1, 7);
INSERT INTO b VALUES (1, 2, UTC_DATE()-1, 12);

SELECT A.id,SUM(B.count) AS total_count,SUM(Y.count) AS y FROM a AS A 
LEFT JOIN b AS B ON (B.a_id=A.id) 
LEFT JOIN b AS Y ON (Y.a_id=A.id AND Y.date=UTC_DATE()-1)
GROUP BY A.id;

Results in:
+----+-------------+------+
| id | total_count | y    |
+----+-------------+------+
|  1 |          68 |   76 |
+----+-------------+------+


The correct result should be:
+----+-------------+------+
| id | total_count | y    |
+----+-------------+------+
|  1 |          34 |   22 |
+----+-------------+------+

What’s going on here? Is this a bug in mysql or am I not understanding how the joins are working.

  • 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-15T21:07:39+00:00Added an answer on June 15, 2026 at 9:07 pm

    No, it’s not a bug in MySQL.

    Your JOIN conditions are generating “duplicate” rows. (Remove the aggregate functions and the GROUP BY, and you’ll see what’s happening.

    That row from table “a” is matching four rows from table “b”. That’s all fine and good. But when you add the join to the third table (“y”), each row returned from that third “y” table (two rows) is being “matched” to every row from the “b” table… so you wind up with a total of eight rows in your result set. (That’s why the “total_count” is getting doubled.)

    To get the result set you specify, you don’t need to join that table “b” second time. Instead, just use a conditional test to determine whether that “count” should be included in the “y” total or not.

    e.g.

    SELECT a.id
         , SUM(b.count) AS total_count
         , SUM(IF(b.date=UTC_DATE()-1 ,b.count,0)) AS y
      FROM a a
      LEFT
      JOIN b b ON (b.a_id=a.id)
     GROUP BY a.id;
    

    Note that the MySQL IF expression can be replaced with an equivalent ANSI CASE expression for improved portability:

         , SUM(CASE WHEN b.date=UTC_DATE()-1 THEN b.count ELSE 0 END) AS y
    

    If you did want to do JOIN to that “b” table a second time, you would want the JOIN condition to be such that a row from “y” would match, at most, ONE row from “b”, so as not to introduce any duplicates. So you’d basically need the join condition to include all of the columns in the primary key.

    (Note that the predicates in the join condition for table “y” guarantee that each from from “y” will match no more than ONE row from “b”):

    SELECT a.id
         , SUM(b.count) AS total_count
         , SUM(y.count) AS y
      FROM a a
      LEFT
      JOIN b b
        ON b.a_id=a.id
      LEFT
      JOIN b y 
        ON y.a_id = b.a_id
       AND y.b_id = b.b_id
       AND y.date = b.date
       AND y.date = UTC_DATE()-1
     GROUP BY a.id;
    

    (To get the first statement to return an identical resultset, with a potential NULL in place of a zero, you’d need to replace the ‘0’ constant in the IF expression with ‘NULL’.

         , SUM(IF(b.date=UTC_DATE()-1 ,b.count,NULL)) AS y
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm seeing basically the same issue described here I have a table that starts
Basically I have a database with two tables, that is, Updates table and Images
I have an application that shows data from a MySQL table. Basically, my application
I have a query that basically combines tables of actions and selects from them
So I'm attempting to do a query on a table that holds two foreign
I have a column called THE_VALUE in a table TABLE_A , that holds data
I have a table that holds availability status for workers. Here is the structure:
i have a table called propAmenities which holds two column amenity_id and property_id basically
The StringMapBase SQL table is the table that holds Option List values that have
I have this temp table that holds the data to be modified in table1,

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.