Solved

Intricate MySQL Query Assistance

Posted on 2008-06-16
4
312 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 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
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…

697 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