Solved

Intricate MySQL Query Assistance

Posted on 2008-06-16
4
302 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
  • 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 500 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now