• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

Intricate MySQL Query Assistance

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
tjcarr
Asked:
tjcarr
  • 2
  • 2
1 Solution
 
bplantCommented:
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
 
tjcarrAuthor Commented:
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
 
bplantCommented:
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
 
tjcarrAuthor Commented:
The "c1.*" did the trick!  Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now