重写此不包含子查询,以便我可以添加到视图

时间:2017-03-12 03:25:42

标签: mysql

请原谅我的无知 - 我和MySQL一起学习,我真的坚持这个:

我刚刚得到了这个查询,但是当我尝试将它添加到视图中时,由于子查询,我需要它才能运行。我无法解决如何避免子查询的问题 - 你们中任何一个知识渊博的人能指出我的正确方向吗?

(SELECT `clients`.`Name` AS `ClientName`, `computers`.`Name` AS `ComputerName`, BackupName, results.Source, results.Result, results.Message, results.TimeGen, results.EventID
FROM 
(
SELECT DISTINCT 
  eventlogs.`ComputerID`, (
    CASE
      WHEN `eventlogs`.message LIKE '% job %'     
    THEN SUBSTR(`eventlogs`.`Message`, (LOCATE(' job ', `eventlogs`.`Message`) + 6), ((LOCATE('finished', `eventlogs`.`Message`) - LOCATE(' job ', `eventlogs`.`Message`)) - 8)) 
      ELSE NULL 
    END
  ) AS `BackupName`, `eventlogs`.`Source` AS `Source`, (
    CASE
      WHEN `eventlogs`.`message` LIKE 'Endpoint%Backup job % finished with Success%'
    THEN 'Pass' 
      WHEN `eventlogs`.`message` LIKE 'Endpoint%Backup job % finished with Warning%'
    THEN 'Pass-Warn' 
      WHEN `eventlogs`.`message` LIKE 'Endpoint%Backup job % finished with Fail%'      
    THEN 'FAIL' 
      WHEN `eventlogs`.`message` LIKE 'Endpoint%Backup job % finished with Error%'
    THEN 'FAIL' 
      ELSE 'Unknown - ASK ALEX!' 
    END
  ) AS `Result`, `eventlogs`.`Message` AS `Message`, `eventlogs`.`TimeGen` AS `TimeGen`, `eventlogs`.`EventID` AS `EventID` 

  FROM `eventlogs` 

  WHERE (
    `eventlogs`.`TimeGen` >= (NOW() - INTERVAL 1 DAY) 
    AND eventlogs.source = "Veeam Endpoint Backup" 
    AND EventID < 10000
    AND EventID != 110
    AND EventID != 1
  )
) AS results
INNER JOIN
(
  SELECT eventlogs.`ComputerID`,
  (CASE
  WHEN `eventlogs`.message LIKE '% job %'     
THEN SUBSTR(`eventlogs`.`Message`, (LOCATE(' job ', `eventlogs`.`Message`) + 6), ((LOCATE('finished', `eventlogs`.`Message`) - LOCATE(' job ', `eventlogs`.`Message`)) - 8)) 
  ELSE NULL 
END) AS `BackupName`, `eventlogs`.`Source` AS `Source`, eventlogs.Message, MAX(TimeGen) AS TimeGen, EventID
 FROM   `eventlogs` 

WHERE (
`eventlogs`.`TimeGen` >= (NOW() - INTERVAL 1 DAY) 
AND eventlogs.source = "Veeam Endpoint Backup" 
AND EventID < 10000
AND EventID != 110
AND EventID != 1 )
 GROUP BY ComputerID, Source, BackupName) AS latest USING (ComputerID, Source, BackupName, TimeGen)
JOIN `computers` USING (ComputerID)
JOIN `clients` USING (ClientID)
)

基本上对于每个(计算机+ BackupName)我需要最近的事件,然后我做出判断并输出结果。不幸的是,BackupName是从eventlogs.Message列中的文本解析的。

对不起,它相当长。欢迎任何帮助或建议 - 谢谢!

1 个答案:

答案 0 :(得分:0)

意识到我可以完全重写并且只是将jusgements应用于来自&#34;最新&#34;的数据。没有复杂性的数据集。只是希望这实际上做了我想要的实践LOL。谢谢戈登。

SELECT `clients`.`Name` AS `ClientName`, `computers`.`Name` AS `ComputerName`, (CASE
  WHEN `eventlogs`.message LIKE '% job %'     
THEN SUBSTR(`eventlogs`.`Message`, (LOCATE(' job ', `eventlogs`.`Message`) + 6), ((LOCATE('finished', `eventlogs`.`Message`) - LOCATE(' job ', `eventlogs`.`Message`)) - 8)) 
  ELSE NULL 
END) AS `BackupName`, `eventlogs`.`Source` AS `Source`, (
    CASE
      WHEN `eventlogs`.`message` LIKE 'Endpoint%Backup job % finished with Success%'
    THEN 'Pass' 
      WHEN `eventlogs`.`message` LIKE 'Endpoint%Backup job % finished with Warning%'
    THEN 'Pass-Warn' 
      WHEN `eventlogs`.`message` LIKE 'Endpoint%Backup job % finished with Fail%'      
    THEN 'FAIL' 
      WHEN `eventlogs`.`message` LIKE 'Endpoint%Backup job % finished with Error%'
    THEN 'FAIL' 
      ELSE 'Unknown - ASK ALEX!' 
    END
  ) AS `Result`, eventlogs.Message, MAX(TimeGen) AS TimeGen, EventID
FROM    `eventlogs`  JOIN `computers` USING (ComputerID)
JOIN `clients` USING (ClientID)
WHERE (
`eventlogs`.`TimeGen` >= (NOW() - INTERVAL 1 DAY) 
AND eventlogs.source = "Veeam Endpoint Backup" 
AND EventID < 10000
AND EventID != 110
AND EventID != 1 )
GROUP BY ComputerID, Source, BackupName