用于过滤匹配结果的SQL查询

时间:2012-10-24 20:18:19

标签: sql sql-server-2008 select

        SELECT TOP 92 PERCENT 
        Convert(DateTime,Floor(Cast((DateTime) as Float)*24)/24,0) AS SummaryDateTime,
        MIN(APM_ApplicationAvailability.PercentAvailability) AS MIN_of_ApplicationAvailability,
        Nodes.Caption AS NodeName 

        FROM 
        (Nodes INNER JOIN APM_AlertsAndReportsData ON (Nodes.NodeID = APM_AlertsAndReportsData.NodeId))  INNER JOIN APM_ApplicationAvailability ON (APM_AlertsAndReportsData.ApplicationId = APM_ApplicationAvailability.ApplicationID)


        WHERE 
        ( DateTime BETWEEN 40907 AND 41205 )
         AND  
        (
          (APM_AlertsAndReportsData.ApplicationName LIKE '%HTTP%') AND 
          (Nodes.Caption = 'www.example.com')
        )
GROUP BY Convert(DateTime,Floor(Cast((DateTime) as Float)*24)/24,0), 
Nodes.Caption


ORDER BY SummaryDateTime ASC

以上查询给我以下结果,附在图像上。 enter image description here

我想编写一个查询来过滤掉所有0.00%结果,因此它只会提供0.00%个数据,因为我不关心100%数据。我不知道如何过滤数据。

1 个答案:

答案 0 :(得分:2)

这是您的完整查询吗?您有混合聚合(MIN)和非聚合(Datetime) 我假设你在某个地方有一个GROUP BY,但无论如何,直截了当的方法是对它进行子查询。

SELECT *
FROM (
    SELECT TOP 92 PERCENT
        Convert(DateTime,Floor(Cast((DateTime) as Float)*24)/24,0) AS SummaryDateTime,
        MIN(APM_ApplicationAvailability.PercentAvailability) AS MIN_of_ApplicationAvailability,
        Nodes.Caption AS NodeName 
    FROM Nodes
    INNER JOIN APM_AlertsAndReportsData
            ON Nodes.NodeID = APM_AlertsAndReportsData.NodeId
    INNER JOIN APM_ApplicationAvailability
            ON APM_AlertsAndReportsData.ApplicationId = APM_ApplicationAvailability.ApplicationID
    WHERE DateTime BETWEEN 40907 AND 41205
      AND APM_AlertsAndReportsData.ApplicationName LIKE '%HTTP%'
      AND Nodes.Caption = 'www.example.com'
    -- GROUP BY..
) X
WHERE MIN_of_ApplicationAvailability = 0.0

通常,对于聚合查询,您还可以使用HAVING子句,例如

    SELECT TOP 92 PERCENT
        Convert(DateTime,Floor(Cast((DateTime) as Float)*24)/24,0) AS SummaryDateTime,
        MIN(APM_ApplicationAvailability.PercentAvailability) AS MIN_of_ApplicationAvailability,
        Nodes.Caption AS NodeName 
    FROM Nodes
    INNER JOIN APM_AlertsAndReportsData
            ON Nodes.NodeID = APM_AlertsAndReportsData.NodeId
    INNER JOIN APM_ApplicationAvailability
            ON APM_AlertsAndReportsData.ApplicationId = APM_ApplicationAvailability.ApplicationID
    WHERE DateTime BETWEEN 40907 AND 41205
      AND APM_AlertsAndReportsData.ApplicationName LIKE '%HTTP%'
      AND Nodes.Caption = 'www.example.com'
    GROUP BY DateTime, Nodes.Caption
    HAVING MIN(APM_ApplicationAvailability.PercentAvailability) = 0.0