?
Solved

Assistance on A Difficult MySQL Query

Posted on 2008-06-13
10
Medium Priority
?
270 Views
Last Modified: 2010-08-05
Hello.  I've got a heck of a SQL question.  I have a SQL table that contains a list backup job successes and failures for each day - there is one row per job result.  I am trying to create a SQL query (I'm running MySQL 5) that will show what the status of each backup job for the past 7 days on one row.  This needs to be a dynamic result so that when I talk about "Today's Date" below, it refers to today, and then the next 6 columns show yesterday's date, the day before, the day before that, etc.  Also, if there is no result for a certain date, that cell needs to be blank.

Here's how my source table (Last_Week_Backup) is laid out:

Machine Name    Backup Job Name      Backup Date    Result
Machine1            BUJob1                      2008-06-12      Success
Machine1            BUJob2                      2008-06-12      Failure
Machine1            BUJob1                      2008-06-11      Success
Machine1            BUJob1                      2008-06-10      Success
Machine1            BUJob2                      2008-06-10      Success
Machine2            BUJob1                      2008-06-12      Success
Machine2            BUJob1                      2008-06-11      Failure
Machine2            BUJob1                      2008-06-10      Failure

I'm trying to get a result query that looks like this:

Machine_Name    Backup_Name      Result Day -1   Result Day-2       Result Day-3   <etc>
Machine1            BUJob1                  Success          Success             Success
Machine1            BUJob2                  Failure              <Blank>               Success
Machine2            BUJob1                  Success           Failure                Failure

I've gotten close (see the code snippet) but that query doesn't return any rows if I'm missing data for any day that I'm trying to grab.
select 
   b1.MACHINE_NAME, 
   b1.BACKUP_NAME,
   b1.REMARKS as "Result Day -1",
   b2.REMARKS as "Result Day -2",
   b3.REMARKS as "Result Day -3",
from 
   LAST_WEEK_BACKUP b1 
Left outer Join 
   LAST_WEEK_BACKUP b2               
on
   (b1.MACHINE_NAME = b2.MACHINE_NAME and b1.BACKUP_NAME = b2.BACKUP_NAME)
Left outer Join
   LAST_WEEK_BACKUP b3 
on       
  (b1.MACHINE_NAME = b3.MACHINE_NAME and b1.BACKUP_NAME = b3.BACKUP_NAME)
where
  b1.start_time = '2008-06-10' and
  b2.start_time = '2008-06-09' and
  b3.start_time = '2008-06-08'
order by
  b1.MACHINE_NAME,
  b2.BACKUP_NAME;

Open in new window

0
Comment
Question by:tjcarr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 21784601
please try this:
select 
   b1.MACHINE_NAME, 
   b1.BACKUP_NAME,
   MAX(case when start_time = '2008-06-12' THEN b1.REMARKS END)as "Result Day -1",
   MAX(case when start_time = '2008-06-11' THEN b1.REMARKS END)as "Result Day -2",
   MAX(case when start_time = '2008-06-10' THEN b1.REMARKS END)as "Result Day -3",
from LAST_WEEK_BACKUP b1 
GROUP BY b1.MACHINE_NAME, b1.BACKUP_NAME
order by  b1.MACHINE_NAME, b1.BACKUP_NAME;

Open in new window

0
 

Author Comment

by:tjcarr
ID: 21785682
That seems to work beautifully!  Thank you.

One question about this, though.  If I have multiple messages for a machine name / backup job combination in one day, is there a way to just select the last one for that day?
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 21786346
OK, because it is a table for backups and volumes shouldn't be too onerous, then, could use in_line queries:

might need to change the date calc to be: date_add(max(b.start_time), - interval 1 day)
select b.MACHINE_NAME,  b.BACKUP_NAME, max(b.start_time) as most_recent,
(select top 1 d1.REMARKS from last_week_backup d1 where d1.machine_name = b.machine_name and d1.backup_name = b.backup_name and d1.start_time = max(b.start_time))as "Results Day - 1",
(select top 1 d1.REMARKS from last_week_backup d1 where d1.machine_name = b.machine_name and d1.backup_name = b.backup_name and d1.start_time = max(b.start_time)-1)as "Results Day - 2",
(select top 1 d1.REMARKS from last_week_backup d1 where d1.machine_name = b.machine_name and d1.backup_name = b.backup_name and d1.start_time = max(b.start_time)-2)as "Results Day - 3"
from LAST_WEEK_BACKUP b
GROUP BY b.MACHINE_NAME, b.BACKUP_NAME
order by b.MACHINE_NAME, b.BACKUP_NAME;

Open in new window

0
PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

 
LVL 51

Expert Comment

by:Mark Wills
ID: 21786352
Oh, and if date time, then will need to look at just the date part of start_time, and also forgot an     order by d1.start_time desc     within the in_line queries...
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21786666
>mark_wills: the TOP syntax is MS SQL Server, won't work in MySQL :)

I am not sure how I would do that in MySQL, actually...
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 21787047
yeah, put "limit 1" at the end of the sub-query not "top 1" straight after the select - fall into that every time...


(select d1.REMARKS from last_week_backup d1 where d1.machine_name = b.machine_name and d1.backup_name = b.backup_name and d1.start_time = max(b.start_time)  order by d.start_time desc limit 1)as "Results Day - 1",
0
 

Author Closing Comment

by:tjcarr
ID: 31467144
Angellll - Thanks.  This works for me; I'll run it against a view that only shows the latest entry for each backup job per day.
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 21790514
Hi tjcarr,

No arguments abouts points or anything, would like to know if you tried my query... Here it is again, with the limit and casting start_time just as date to remove time comppnents (not sure if it was necessary, but the column name suggests it is) if you have time, would like some feedback...
select b.MACHINE_NAME,  b.BACKUP_NAME, max(b.start_time) as most_recent,
(select d1.REMARKS from last_week_backup d1 where d1.machine_name = b.machine_name and d1.backup_name = b.backup_name and date(d1.start_time) = (max(date(b.start_time))) order by d.start_time desc limit 1)as "Results Day - 1",
(select d1.REMARKS from last_week_backup d1 where d1.machine_name = b.machine_name and d1.backup_name = b.backup_name and date(d1.start_time) = (max(date(b.start_time))-1) order by d.start_time desc limit 1)as "Results Day - 2",
(select d1.REMARKS from last_week_backup d1 where d1.machine_name = b.machine_name and d1.backup_name = b.backup_name and date(d1.start_time) = (max(date(b.start_time))-2) order by d.start_time desc limit 1)as "Results Day - 3"
from LAST_WEEK_BACKUP b
GROUP BY b.MACHINE_NAME, b.BACKUP_NAME
ORDER BY b.MACHINE_NAME, b.BACKUP_NAME;

Open in new window

0
 

Author Comment

by:tjcarr
ID: 21793721
mark_wills:
Thanks for your response and apologies for not responding back to you faster.  Unfortunately, the query you provided actually only showed "nulls" for all the "results day-2" and "results day-3" columns.  Also, that query took about 3 times as long as angelll's response.
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 21794342
Thanks for giving it a run. Kind of expected it to be slower, wasn't overly concerned considering it was a table of backup data - think I mentioned that from the getgo...

There will be an error somewhere with the datetime casting or, subtracting 1 from day - should have used add_date - just being lazy.

Just wanted to confirm some of the syntax, which you were able to do for me. Thanks again for running it...

Cheers,

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question