We help IT Professionals succeed at work.
Get Started

MySQL Union select statements

that1guy15
that1guy15 asked
on
616 Views
Last Modified: 2012-05-09
Hey all,

First off I am not by any means a DBA or a database guy. I can stumble around in MySQL enough to get what i want and break stuff :)

I have a database that is used for system monitoring. Events are entered into one table (status) and then over time "aged out" and moved to a second table (history). I have a query that will return the total number of each event over a given time frame. I use this to basically get a top 10 errors report for a given time frame.

This works great when i run the query against one table or the other. My problem is when i use the UNION ALL statement and combine the two into a single query. the query runs fine and gives me the proper results for each table. But it does not merge the same events from each table. So the returned list has duplicate events, one from each table. I would like to get only a single event for both tables and its combined count.

Here is the query i am running and a sample of what it returns.

Thanks for your help!

SELECT COUNT(summary) AS total_count, summary FROM history 
 WHERE lastTime>(UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 10 HOUR))) 
 AND severity = 3 GROUP BY summary UNION ALL 
SELECT COUNT(summary) AS total_count, summary FROM status 
 WHERE lastTime>(UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 10 HOUR))) 
 AND severity = 3 GROUP BY summary ORDER by total_count DESC;



+-------------+------------------------+
| total_count | summary                |
+-------------+------------------------+
|          60 | TIMEOUT14              |
|          54 | TIMEOUT14              |
|          53 | COMMERROR              |
|          48 | COMMERROR              |
|          48 | DURATIONTIMEOUT        |
+-------------+------------------------+

Open in new window

Comment
Watch Question
Commented:
This problem has been solved!
Unlock 3 Answers and 9 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE