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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:25:26+00:00 2026-06-18T17:25:26+00:00

I have two select statements that take values from two different tables. the select

  • 0

I have two select statements that take values from two different tables.
the select statements look something like this

SELECT year(timestamp) y, month(timestamp) m, count(id) c
FROM table
WHERE clause="foo"
GROUP BY year(timestamp), month(timestamp)

which returns something like

|-y--|-m|c|
|2013|01|9|
|2013|02|9|
|2013|03|9|
|2013|04|9|

.

SELECT year(timestamp) y, month(timestamp) m, count(id) c
FROM table2
WHERE clause="foo"
GROUP BY year(timestamp), month(timestamp)

which returns something like

|-y--|-m|c|
|2013|01|1|
|2013|03|1|
|2013|04|1|

What I’m looking for is joining the two tables based on the timestamp, and subtract the second table from the first. So it should look like:

|-y--|-m|c|
|2013|01|8|
|2013|02|9|
|2013|03|8|
|2013|04|8|

thanks!

  • 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-18T17:25:27+00:00Added an answer on June 18, 2026 at 5:25 pm

    Assuming TableA has your master records, then something like this should work:

    SELECT t.y, t.m, t.c - IFNULL(t2.c,0) c
    FROM
    (
        SELECT year(timestamp) y, month(timestamp) m, count(id) c
        FROM table
        WHERE clause="foo"
        GROUP BY year(timestamp), month(timestamp)
    ) t
    LEFT JOIN (
        SELECT year(timestamp) y, month(timestamp) m, count(id) c
        FROM table2
        WHERE clause="foo"
        GROUP BY year(timestamp), month(timestamp)
    ) t2 ON t.y = t2.y AND t.m = t2.m
    UNION 
    SELECT t.y, t.m, (t.c * -1) c
    FROM
    (
        SELECT year(timestamp) y, month(timestamp) m, count(id) c
        FROM table2
        WHERE clause="foo"
        GROUP BY year(timestamp), month(timestamp)
    ) t
    LEFT JOIN (
        SELECT year(timestamp) y, month(timestamp) m, count(id) c
        FROM table
        WHERE clause="foo"
        GROUP BY year(timestamp), month(timestamp)
    ) t2 ON t.y = t2.y AND t.m = t2.m
    WHERE t2.y IS NULL
    ORDER BY y, m
    

    I’ve updated the answer. The first part of the union returns all records in table with or without those matching in table2, subtracting the table2 count when available. The second part of the union returns all records in table2 that do not exist in table multiplying the count column by -1.

    There may be a simpler approach, but without really understanding the tables, this is the best I can offer. And unfortunately MySQL doesn’t support FULL OUTER JOINs which would alleviate the need for the UNION.

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

Sidebar

Related Questions

I have two SELECT statements that return a number each, and I'd like to
I have two select statements that I am trying to port from Oracle to
I currently have two fairly long select statements, identical apart from the fact that
So I have two multiple select boxes like this <select id=foo multiple=multiple> <option value=1>Option
I have two separate SELECT statements: SELECT VCe.VId FROM `VCe` WHERE `YId` = 9007
Let's say.. I have two statements select min(Log_In_Time) from tbl where (event_ID=4) select max(Log_Off_Time)
I have two select statements that find the maximum salary for a specific role.
I have a table on SQL server and two Select statements with different WHERE
I currently have two SQL statements that I'd like to combine into one, and
I want to join two or more select statements that have a group by

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.