tjcarr
asked on
Intricate MySQL Query Assistance
Hello. This is a followup to the question I posted here (https://www.experts-exchange.com/questions/23484583/Assistance-on-A-Difficult-MySQL-Query.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!
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
;
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_N AME"
ASKER
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The "c1.*" did the trick! Thanks.