1

我有以下 SQL 查询

SELECT TOP 10000 AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability
FROM Nodes 
INNER JOIN DailyNodeAvailability 
    ON (Nodes.NodeID = DailyNodeAvailability.NodeID)
WHERE 
( DateTime > (GetDate()-7) )
 AND  
(
  (
  (Nodes.Caption LIKE '%server1%') OR 
  (Nodes.Caption LIKE '%server2%') OR 
  (Nodes.Caption LIKE '%server3%') 
  )
)

它给了我 3 台服务器的平均可用性报告。但我想要最少的可用性。例如。

Server1 = 100%
Server2 = 97%
Server3 = 88%

我希望 SQL 查询结果至少有一个 Server3 = 88%(非平均)

如何修改现有查询以使其正常工作?

有混乱所以,我附上结果的图片。它给了我一个单一的结果,但平均而言,我不想要平均我在我的结果中需要最少的结果值,根据示例。

在此处输入图像描述

4

3 回答 3

2
SELECT MIN(AVERAGE_of_Availability)
FROM
(
SELECT AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability
FROM Nodes 
INNER JOIN DailyNodeAvailability 
    ON (Nodes.NodeID = DailyNodeAvailability.NodeID)
WHERE 
( DateTime > (GetDate()-7) )
 AND  
(
  (
  (Nodes.Caption LIKE '%server1%') OR 
  (Nodes.Caption LIKE '%server2%') OR 
  (Nodes.Caption LIKE '%server3%') 
  )
)
group by Nodes.Caption
) AvgAvailability

这将为您提供最低限度,但它不会告诉您显示的是哪个服务器。要获得服务器名称(Nodes.Caption),我们还需要做更多工作。由于它不在原始查询中,因此我不包括该部分,否则它会使它变得更复杂一些。

于 2012-04-09T14:36:42.987 回答
2

您可以在 SQL Server 中更紧凑地执行此操作,如下所示:

SELECT TOP (1) WITH TIES
  N.Caption AS Server,
  AVG(A.Availability) AS Avg_Availability
FROM Nodes AS N
JOIN DailyNodeAvailability AS A
ON (N.NodeID = A.NodeID)
WHERE DateTime > (GetDate()-7)
AND N.Caption LIKE '%server[123]%'
GROUP BY N.Caption
ORDER BY AVG(A.Availability)
于 2012-04-09T20:40:29.133 回答
2

您的基本查询应写为:

SELECT N.Caption AS Server, AVG(A.Availability) AS Avg_Availability
  FROM Nodes AS N
  JOIN DailyNodeAvailability AS A ON (N.NodeID = A.NodeID)
 WHERE DateTime > (GetDate()-7)
   AND (N.Caption LIKE '%server1%' OR
        N.Caption LIKE '%server2%' OR
        N.Caption LIKE '%server3%'
       )
 GROUP BY N.Caption

这将为您提供一个列表,其中包含每个服务器名称以及该服务器在上周的平均可用性。

要获得具有最低平均可用性及其平均可用性的服务器名称,您必须将该查询视为更大查询的一部分。在标准 SQL 中有两种方法可以做到这一点:冗长且普遍可用,以及简洁但不太广泛可用。

冗长的方式最终将该查询作为子查询写出几次(因此,在开发查询时,在继续执行更复杂的查询之前,让第一个查询正确并经过测试至关重要):

SELECT m.server, m.avg_availability
  FROM (SELECT N.Caption AS Server, AVG(A.Availability) AS Avg_Availability
          FROM Nodes AS N
          JOIN DailyNodeAvailability AS A ON (N.NodeID = A.NodeID)
         WHERE DateTime > (GetDate()-7)
           AND (N.Caption LIKE '%server1%' OR
                N.Caption LIKE '%server2%' OR
                N.Caption LIKE '%server3%'
               )
         GROUP BY N.Caption
       ) AS m
  JOIN (SELECT MIN(y.avg_availability) AS min_availability
          FROM (SELECT N.Caption AS Server, AVG(A.Availability) AS Avg_Availability
                  FROM Nodes AS N
                  JOIN DailyNodeAvailability AS A ON (N.NodeID = A.NodeID)
                 WHERE DateTime > (GetDate()-7)
                   AND (N.Caption LIKE '%server1%' OR
                        N.Caption LIKE '%server2%' OR
                        N.Caption LIKE '%server3%'
                       )
                 GROUP BY N.Caption
               ) AS Y
       ) AS x
    ON x.min_availability = m.avg_availability

简洁的方法是使用公用表表达式,即 WITH 子句:

WITH AvgAvailability AS
    (
    SELECT N.Caption AS Server, AVG(A.Availability) AS Avg_Availability
      FROM Nodes AS N
      JOIN DailyNodeAvailability AS A ON (N.NodeID = A.NodeID)
     WHERE DateTime > (GetDate()-7)
       AND (N.Caption LIKE '%server1%' OR
            N.Caption LIKE '%server2%' OR
            N.Caption LIKE '%server3%'
           )
     GROUP BY N.Caption
    )
SELECT A.Server, A.Avg_Availabilty
  FROM AvgAvailability AS A
  JOIN (SELECT MIN(Avg_Availability) AS Min_Availability
          FROM AvgAvailability
       ) AS M
    ON A.Avg_Availability = M.Min_Availability

(未经测试的语法。)我不确定 MS SQL Server 中是否提供简洁选项。

于 2012-04-09T14:54:44.250 回答