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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:06:24+00:00 2026-05-25T19:06:24+00:00

I have a mysql db/server that has 3 tables that are identical in structure:

  • 0

I have a mysql db/server that has 3 tables that are identical in structure:
west, midwest and east.

I would like to create a national table with the sum of the columns of those regional tables, ONLY if the datetime row matches all 3 tables. That way if one hour is missing in a particular table, I don’t end up summing 2 regions and calling it national.

Here is how I am thinking to do it:

All 3 tables have a datetime column.

Merge the tables (union?) only if the datetime row exists in all 3 tables.

Aggregate (sum) the columns grouped by datetime column. I would of course be summing all columns which carry int values.

I am not sure how to run a query that would perform this task.
These tables have 11mil rows so an efficient way would be great.
I am also open to other approaches to solve this problem.


I picked the answer from Neil because although the answer would not work if datetime col is not unique i.e. multiple rows in Table1 with the same datetime. Using any other method the performance I got was horrific, hours of query time. I decided to compromise. I created 3 new tables
westh, midwesth and southh.
These 3 new tables are a creation of aggregating the original tables by hour.
I then used Neils second version with a twist:

INNER JOIN Table2 USING (datetime)

While datetime is indexed in my tables that provides superior performance which is a firm criteria for me.

  • 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-25T19:06:25+00:00Added an answer on May 25, 2026 at 7:06 pm

    First version:

    SELECT T123.dtcol, SUM(T123.intcol) AS intcolsum
      FROM (
        SELECT Table1.dtcol, Table1.intcol FROM Table1
        UNION
        SELECT Table2.dtcol, Table2.intcol FROM Table2
        UNION
        SELECT Table3.dtcol, Table3.intcol FROM Table3
      ) T123
      GROUP BY T123.dtcol
      HAVING COUNT(*) = 3
    

    Second version:

    SELECT Table1.dtcol, Table1.intcol + Table2.intcol + Table3.intcol AS intcolsum
      FROM Table1 T1
      INNER JOIN Table2 T2 ON T2.dtcol = T1.dtcol
      INNER JOIN Table3 T2 ON T3.dtcol = T1.dtcol
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hey, I have a c# server-side app that has db (mysql), and quite oftenly,
I have an PHP/MySQL eCommerce application that has a few innoDB tables (for the
I have a fairly small MySQL database (a Textpattern install) on a server that
I have an SSIS package that connects to a mysql server and attempts to
I have this mySQL code that connects to my server. It connects just fine:
I have a CakePHP app that is being moved to Sql Server from MySql.
Does Microsoft Access have Full Text Search? I know that MySQL and SQL Server
My MySql server currently has 235 databases. Should I worry? They all have same
What is the best way to optimize a MYSQL server that has 400-1000 queries
I have currently created a facebook like page that pulls notifications from different tables,

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.