AVG函数PostgreSQL

时间:2019-12-16 14:23:50

标签: sql postgresql

我有这个查询

SELECT humidity_float 
FROM   labs_climate_measuring_room 
       INNER JOIN labs_climate_measuring 
               ON labs_climate_measuring.id = 
                  labs_climate_measuring_room.measuring_id 
       INNER JOIN labs_room 
               ON labs_room.id = labs_climate_measuring_room.room_id 
WHERE  labs_climate_measuring_room.room_id = 81 
       AND labs_climate_measuring.action_time <= '2019-12-18 06:00:00' 
       AND labs_room.is_conditioning_room = true 
       AND humidity_float IS NOT NULL 
ORDER  BY labs_climate_measuring.action_time 

将返回两行。我需要得到它的平均值。所以我这样做

SELECT avg(humidity_float) OVER (PARTITION BY temperature_float) AS cum_amt

这还将返回两行。如何获得平均价值?

1 个答案:

答案 0 :(得分:0)

您不需要PARTITION,只需进行汇总:

SELECT AVG(humidity_float) AS cum_amt
FROM labs_climate_measuring_room 
INNER JOIN labs_climate_measuring ON labs_climate_measuring.id = labs_climate_measuring_room.measuring_id
INNER JOIN labs_room ON labs_room.id = labs_climate_measuring_room.room_id 
WHERE labs_climate_measuring_room.room_id = 81
and labs_climate_measuring.action_time <= '2019-12-18 06:00:00'
AND labs_room.is_conditioning_room = TRUE
AND humidity_float IS NOT NULL

而且您也不需要ORDER BY。