我有一张包含以下内容的表格:
ID Node LoadTime DateTimeCST Failed
-----------------------------------------
我正在尝试编写一个返回下表的查询(Node是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?任何帮助将不胜感激。谢谢。
答案 0 :(得分: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
答案 1 :(得分: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