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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:58:13+00:00 2026-05-13T09:58:13+00:00

I’m looking to get the max discrepancy between two tables per day, per id.

  • 0

I’m looking to get the max discrepancy between two tables per day, per id. I have the following data in a mysql database

insert into test.foo values ('2010-01-10', 1, 10);
insert into test.foo values ('2010-01-10', 1, 5);
insert into test.foo values ('2010-01-10', 2, 10);
insert into test.foo values ('2010-01-10', 2, 10);
insert into test.foo values ('2010-01-10', 3, 15);
insert into test.foo values ('2010-01-10', 3, 15);
insert into test.foo values ('2010-01-11', 1, 5);
insert into test.foo values ('2010-01-11', 1, 5);
insert into test.foo values ('2010-01-11', 2, 5);
insert into test.foo values ('2010-01-11', 2, 5);
insert into test.foo values ('2010-01-11', 3, 5);
insert into test.foo values ('2010-01-11', 3, 5);

insert into test.bar values ('2010-01-10', 1, 5);
insert into test.bar values ('2010-01-10', 1, 5);
insert into test.bar values ('2010-01-10', 2, 5);
insert into test.bar values ('2010-01-10', 2, 5);
insert into test.bar values ('2010-01-10', 3, 5);
insert into test.bar values ('2010-01-10', 3, 5);
insert into test.bar values ('2010-01-11', 1, 10);
insert into test.bar values ('2010-01-11', 1, 10);
insert into test.bar values ('2010-01-11', 2, 5);
insert into test.bar values ('2010-01-11', 2, 5);
insert into test.bar values ('2010-01-11', 3, 5);
insert into test.bar values ('2010-01-11', 3, 5);

Here is my query:

SELECT t1.`date`, t1.id, t1.sums, t2.sums, max(t1.sums - t2.sums) FROM
  (select `date`, id, sum(val) sums
   from test.foo
   group by `date`, id) as t1,
  (select `date`, id, sum(val) sums
   from test.bar
   group by `date`, id) as t2
WHERE t1.`date` = t2.`date` AND t1.id = t2.id
group by t1.`date`

I’m getting this result:

+---------------------+----+------+------+------------------------+
| date                | id | sums | sums | max(t1.sums - t2.sums) |
+---------------------+----+------+------+------------------------+
| 2010-01-10 00:00:00 |  1 |   15 |   10 |                     20 |
| 2010-01-11 00:00:00 |  1 |   10 |   20 |                      0 |
+---------------------+----+------+------+------------------------+
2 rows in set (0.00 sec)

I’d like to be getting this result:
I’m getting this result:

+---------------------+----+------+------+------------------------+
| date                | id | sums | sums | max(t1.sums - t2.sums) |
+---------------------+----+------+------+------------------------+
| 2010-01-10 00:00:00 |  1 |   15 |   10 |                     20 |
| 2010-01-11 00:00:00 |  2 |   10 |   10 |                      0 |  <----- 
+---------------------+----+------+------+------------------------+

Can anyone help me? I’d like to be getting the max difference, and then the line that went along with it. This query gives me the correct difference, but not the id and sums that go with it. A colleague suggested also grouping by id, but as I thought, that just flattened out the result and each id was listed instead of the one id for the day that had the max difference.

Thanks much in advance

  • 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-13T09:58:13+00:00Added an answer on May 13, 2026 at 9:58 am

    This one should work for you.

    It sorts the sums in descending order, assigning them a rank, and then only get those with rank=1.

    SELECT id, `date`, sums FROM (
      SELECT id, `date`, sums,
      CASE
        WHEN @d != `date` THEN @rownum := 1 
        ELSE @rownum := @rownum + 1
      END AS rank,
      @d := `date`
    FROM
    (
      SELECT t1.`date`, t1.id, t1.sums t1_sums, t2.sums t2_sums, (t1.sums - t2.sums) sums
      FROM
        (select `date`, id, sum(val) sums
         from foo
         group by `date`, id) as t1,
        (select `date`, id, sum(val) sums
         from bar
         group by `date`, id) as t2,
         (SELECT @rownum := 0, @d := NULL) r
      WHERE t1.`date` = t2.`date` AND t1.id = t2.id
      GROUP BY t1.`date`, t1.id, t2.`date`, t2.id
      ORDER BY t1.`date`, (t1.sums - t2.sums) DESC, t1.id
      ) x
    ) y
    WHERE rank = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.