0

我有一个包含以下内容的表:

ID   Node   LoadTime   DateTimeCST   Failed
-----------------------------------------

我正在尝试编写一个返回下表的查询(节点为 DISTINCT)。

Node    TodaysAverageLoadTime    HistoricalLoadTime
----------------------------------------------------

所以,我基本上试图取过去 24 小时的 LoadTime 平均值,然后是 LoadTime 的另一个平均值,该平均值是 LoadTime 表中所有数据的平均值。这是我用来获取今天平均值的查询。

SELECT DISTINCT Node, AVG(LoadTime)
FROM dbo.Table
WHERE Failed != 1
AND DATEDIFF(day, DateTimeCST, GETDATE()) = 0 
GROUP BY Node

我需要在此查询中添加什么来平均所有记录的 LoadTime?任何帮助将不胜感激。谢谢。

4

2 回答 2

1

由于 TodaysAverageLoadTime 和 HistoricalLoadTime 的记录识别标准不同,因此不能在单个查询中完成。但是你可以模拟如下:

SELECT 
       Node,
       ISNULL(TodaysAvgLoadTime, 0)
       ISNULL(HistoricalLoadTime, 0)
FROM 
        (
            SELECT 
                 Node, 
                 AVG(LoadTime) 'TodaysAvgLoadTime'
            FROM 
                 dbo.Table
            WHERE 
                 Failed != 1
            AND  DATEDIFF(day, DateTimeCST, GETDATE()) = 0 
            GROUP BY Node
        ) AS A
        FULL OUTER JOIN
        (
            SELECT 
                 Node, 
                 AVG(LoadTime) 'HistoricalLoadTime'
            FROM 
                 dbo.Table
            WHERE 
                 Failed != 1
            GROUP BY Node
        ) AS B
        ON A.Node = B.Node
于 2012-08-24T17:02:44.057 回答
1

您可以使用多个子查询来获取数据并在它们之间进行连接。所以像:

SELECT Node, t1.avg, t2.avg FROM
    (SELECT DISTINCT Node, AVG(LoadTime) AS avg
    FROM dbo.Table, 
    WHERE Failed != 1
    AND DATEDIFF(day, DateTimeCST, GETDATE()) = 0 
    GROUP BY Node) t1, 
    (SELECT DISTINCT Node, AVG(LoadTime) AS avg
    FROM dbo.Table, 
    WHERE Failed != 1
    GROUP BY Node) t2
WHERE t1.node = t2.node
于 2012-08-24T17:04:03.520 回答