我有以下查询:
SELECT `station_id`, AVG(`jan`) AS avg_jan, AVG(`feb`) AS avg_feb, ... ,
AVG(`dec`) AS avg_dec
FROM `climate_data`
WHERE `element_name` = "Temp_mean_mly"
AND `jan` <> -999999
AND `feb` <> -999999
AND ...
AND `dec` <> -999999
GROUP BY station_id
我需要它为每个列单独返回不同于-999999的平均值!当前查询将删除所有具有值-999999且不正确的列的行。
由于
答案 0 :(得分:2)
你可以这样使用NULLIF
:
SELECT
station_id,
AVG(NULLIF(jan, -99999)) AS avg_jan,
AVG(NULLIF(feb, -99999)) AS avg_feb,
...
FROM climate_data
WHERE element_name = 'Temp_mean_mly'
GROUP BY station_id
这是有效的,因为聚合函数会跳过NULL值。