开发者

SQL aggregate with multiple rows

开发者 https://www.devze.com 2023-01-15 06:30 出处:网络
I have the following query SELECT tagindex, AVG(val) from floatTable 开发者_StackOverflow社区 WHERE tagindex IN(828,856,883,910)

I have the following query

SELECT tagindex, AVG(val) from floatTable
开发者_StackOverflow社区 WHERE tagindex IN(828,856,883,910)
   AND DateAndTime > DATEADD(HH,-1,GETDATE()) 
   AND DateAndTime < DATEADD(HH,-2,GETDATE())
group by tagindex

It returns the following:

828   1
856   1
883   1
910   1

How can I return a single result where it is the combined average of all the rows?


SELECT AVG(val) 
FROM floatTable 
WHERE tagindex IN (828, 856, 883, 910) 
    AND DateAndTime > DATEADD(HH, -1, GETDATE()) 
    AND DateAndTime < DATEADD(HH, -2, GETDATE()) 


Select Avg(AverageValue)
From
(
   SELECT tagindex, AVG(val) as AverageValue from floatTable
   WHERE tagindex IN(828,856,883,910)
   AND DateAndTime > DATEADD(HH,-1,GETDATE()) AND DateAndTime < DATEADD(HH,-2,GETDATE())
   group by tagindex
) AverageValues
0

精彩评论

暂无评论...
验证码 换一张
取 消