[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Intricate MySQL Query Assistance

Posted on 2008-06-16
4
Medium Priority
?
316 Views
Last Modified: 2010-08-05
Hello.  This is a followup to the question I posted here (http://www.experts-exchange.com/Database/MySQL/Q_23484583.html)

Basically, I am needing to do a join on a query that isn't working for me.  In the code below, the 2nd query (starting after the left join statement) works fine on its own; it generates a list of backup job results for the past week.  I am trying to join it to an initial query that lists out all the possible backups that could have occurred (I'm pulling this from a different table) as the 2nd query won't show any results if a machine/backup job pair hasn't had any results in the previous week.  I'm doing this to make sure that I have every machine name / backup job that should be backing up actually showing up in the query.

When I run the code below, I get a "Every derived table must have it's own alias" message.  While I can typically troubleshoot simple queries, this one has gotten beyond me - I've tried lots of different combinations but no luck.

Thanks!
select distinct `a1`.`CLIENT_NAME`,`a1`.`BACKUP_NAME` from `SERVER_BACKUP_REPORT` `a1` 
left join
(select `b1`.`CLIENT_NAME` AS `CLIENT_NAME`,
`b1`.`BACKUP_NAME` AS `BACKUP_NAME`,
max((case when (`b1`.`START_TIME` = curdate()) then `b1`.`REMARKS` end)) AS `Result_Today`,
max((case when (`b1`.`START_TIME` = curdate()) then `b1`.`NO_OF_SKIPPED_FILES` end)) AS `SKP_FILES_TODAY`,
max((case when (`b1`.`START_TIME` = date_format((curdate() - 1),_utf8'%Y-%m-%d')) then `b1`.`REMARKS` end)) AS `Result_Day-1`,
max((case when (`b1`.`START_TIME` = date_format((curdate() - 1),_utf8'%Y-%m-%d')) then `b1`.`NO_OF_SKIPPED_FILES` end)) AS `SKP_FILES_Day-1`,
max((case when (`b1`.`START_TIME` = date_format((curdate() - 2),_utf8'%Y-%m-%d')) then `b1`.`REMARKS` end)) AS `Result_Day-2`,
max((case when (`b1`.`START_TIME` = date_format((curdate() - 2),_utf8'%Y-%m-%d')) then `b1`.`NO_OF_SKIPPED_FILES` end)) AS `SKP_FILES_Day-2`,
max((case when (`b1`.`START_TIME` = date_format((curdate() - 3),_utf8'%Y-%m-%d')) then `b1`.`REMARKS` end)) AS `Result_Day-3`,
max((case when (`b1`.`START_TIME` = date_format((curdate() - 3),_utf8'%Y-%m-%d')) then `b1`.`NO_OF_SKIPPED_FILES` end)) AS `SKP_FILES_Day-3`,
max((case when (`b1`.`START_TIME` = date_format((curdate() - 4),_utf8'%Y-%m-%d')) then `b1`.`REMARKS` end)) AS `Result_Day-4`,
max((case when (`b1`.`START_TIME` = date_format((curdate() - 4),_utf8'%Y-%m-%d')) then `b1`.`NO_OF_SKIPPED_FILES` end)) AS `SKP_FILES_Day-4`,
max((case when (`b1`.`START_TIME` = date_format((curdate() - 5),_utf8'%Y-%m-%d')) then `b1`.`REMARKS` end)) AS `Result_Day-5`,
max((case when (`b1`.`START_TIME` = date_format((curdate() - 5),_utf8'%Y-%m-%d')) then `b1`.`NO_OF_SKIPPED_FILES` end)) AS `SKP_FILES_Day-5`,
max((case when (`b1`.`START_TIME` = date_format((curdate() - 6),_utf8'%Y-%m-%d')) then `b1`.`REMARKS` end)) AS `Result_Day-6`,
max((case when (`b1`.`START_TIME` = date_format((curdate() - 6),_utf8'%Y-%m-%d')) then `b1`.`NO_OF_SKIPPED_FILES` end)) AS `SKP_FILES_Day-6` 
from `SG_LAST_WEEK_BACKUP` `b1` 
group by `b1`.`CLIENT_NAME`,`b1`.`BACKUP_NAME` )
on a1.CLIENT_NAME = CLIENT_NAME and a1.BACKUP_NAME=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
  • 2
  • 2
4 Comments
 
LVL 5

Expert Comment

by:bplant
ID: 21797925
Try changing "on a1.CLIENT_NAME = CLIENT_NAME and a1.BACKUP_NAME=BACKUP_NAME" to "AS `c1` on a1.CLIENT_NAME = c1.CLIENT_NAME and a1.BACKUP_NAME=c1.BACKUP_NAME"
0
 

Author Comment

by:tjcarr
ID: 21798744
Well, the query runs successfully, but it only returns the items from the first table and no results from the second table.  Might that be because there isn't anything explicitly defined as "c1.client_name" and "c1.backup_name"?  Do I have to get that in the 2nd query somehow?
0
 
LVL 5

Accepted Solution

by:
bplant earned 2000 total points
ID: 21798787
Try adding "c1.*" to the select clause. I.e. "select distinct `a1`.`CLIENT_NAME`,`a1`.`BACKUP_NAME`, c1.* from".

Alternatively, you could change the query to "select distinct * from" or list each column from c1 (the sub-query) explicitly.
0
 

Author Closing Comment

by:tjcarr
ID: 31467777
The "c1.*" did the trick!  Thanks.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
In this article, we’ll look at how to deploy ProxySQL.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

650 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