我有以下查询可以在SQL Server 2012上运行,以便为CounterValue
列中的每小时平均值提供一次:
select
dateadd(hour, datediff(hour, 0, CounterDateTime), 0),
avg(CounterValue)
from mopsxactthroughput
group by
dateadd(hour, datediff(hour, 0, CounterDateTime), 0)
如果可能的话,我想修改它,这样如果只有一个记录匹配那个小时,它就不会返回一小时的行。例如,如果在2013-06-06T20:00:00和2013-06-06T21:00:00之间只有一条记录,我希望在一小时内没有返回任何行。
要求提供SELECT
,这对我来说有点太多了,但也许有办法。 (我的后备将返回按小时分组的所有记录,然后在Java中自行迭代行集并在那里应用平均/丢弃。)
答案 0 :(得分:2)
只需添加having
子句:
select
dateadd(hour, datediff(hour, 0, CounterDateTime), 0),
avg(CounterValue)
from mopsxactthroughput
group by
dateadd(hour, datediff(hour, 0, CounterDateTime), 0)
having count(*) > 1
答案 1 :(得分:1)
也许这个CTE有效:
with cte as(
select dateadd(hour, datediff(hour, 0, CounterDateTime), 0) as Hour,
avg(CounterValue)over(partition by dateadd(hour, datediff(hour, 0, CounterDateTime), 0)) as AvgCounter,
Count(*)over(partition by dateadd(hour, datediff(hour, 0, CounterDateTime), 0)) as HourCount,
CounterValue,
CounterDateTime
from mopsxactthroughput
)
select Hour, CounterValue, CounterDateTime, AvgCounter
from cte
where HourCount > 1