调整SQL查询输出每个性能计数器的每周平均值

时间:2012-12-15 18:21:58

标签: sql sql-server-2000

我有一些我想要报告的性能数据。

我需要输出给出每个柜台每个月每周的平均值。

以下是使用日期/时间字段的查询(即'2012-01-01 00:00:00.750')

SELECT MachineName ,
        CounterName ,
        InstanceName, 
        CounterValue,
        ObjectName,
        CounterDateTime 
FROM    dbo.CounterDetails cdt

        INNER JOIN dbo.CounterData cd ON cdt.CounterID = cd.CounterID
        INNER JOIN DisplayToID d ON d.GUID = cd.GUID

WHERE   MachineName = '\\SERVER1'

AND CounterDateTime BETWEEN '2012-01-01 00:00:00.750' AND '2012-01-31 23:59:00.750'
GROUP BY ObjectName, CounterName, InstanceName, CounterValue, CounterDateTime, MachineName
ORDER BY CounterDateTime

感谢回复

1 个答案:

答案 0 :(得分:0)

我重新排列了你的字段,因为它对我没有意义,但你可以试试这个:

SELECT  MachineName
        , ObjectName
        , InstanceName
        , CounterName
        , DATEPART( YEAR, CounterDateTime ) As YearNo
        , DATEPART( MONTH, CounterDateTime ) As MonthNo
        , DATEPART( WEEK, CounterDateTime ) AS WeekNo
        , AVG(CounterValue) AS avgCounterValue
FROM    dbo.CounterDetails cdt
        INNER JOIN dbo.CounterData cd
            ON cdt.CounterID = cd.CounterID
        INNER JOIN DisplayToID d
            ON d.GUID = cd.GUID
WHERE       MachineName = '\\SERVER1'
            AND CounterDateTime BETWEEN '2012-01-01 00:00:00.750' AND '2012-01-31 23:59:00.750'
GROUP BY    MachineName, ObjectName, InstanceName, CounterName
            , DATEPART( YEAR, CounterDateTime )
            , DATEPART( MONTH, CounterDateTime )
            , DATEPART( WEEK, CounterDateTime )
ORDER BY    MachineName, ObjectName, InstanceName, CounterName
            , DATEPART( YEAR, CounterDateTime )
            , DATEPART( MONTH, CounterDateTime )
            , DATEPART( WEEK, CounterDateTime )

如果你的表很大,你可能想在[MachineName]和[CounterDateTime]上创建一个索引