MySQL NULL 值问题

MySQL issue with NULL values

我有一个 table 字段:country_code、short_name、currency_unit、a2010、a2011、a2012、a2013、a2014、a2015。 a2010-a2015 字段是 double 类型。

如何进行查询以按字段 a2010-a2015 的平均值对结果进行排序,同时记住这些字段可能具有 NULL 值?

我试过这段代码,但没有用(returns 一个错误,它告诉我们 ORDER BY 部分有问题。错误是在说一些关于 coumn names 和 GROUP BY 的事情)。逻辑是:ORDER BY ((A)/(B)) 其中 A - 非空字段的总和和 B - 非空字段的计数。

有什么想法吗?

(如果重要,代码将在 BigInsights 环境中使用)

SELECT country_code, short_name, currency_unit, a2010, a2011, a2012, 
a2013, a2014, a2015
FROM my_schema.my_table
WHERE Indicator_Code = 'SE.PRM.TENR'
ORDER BY 
(
(
Coalesce(a2010,0) + Coalesce(a2011,0) + Coalesce(a2012,0)  
+Coalesce(a2013,0) + Coalesce(a2014,0) + Coalesce(a2015,0)
)
/
(
COUNT(Coalesce(a2010)) + COUNT(Coalesce(a2011)) + COUNT(Coalesce(a2012)) 
+ COUNT(Coalesce(a2013)) + COUNT(Coalesce(a2014)) + 
COUNT(Coalesce(a2015))
)
) DESC;

使用MySQLifnull

IFNULL(expression_1,expression_2)

在您的查询中:-[​​=13=]

IFNULL(
(
COUNT(Coalesce(a2010)) + COUNT(Coalesce(a2011)) + COUNT(Coalesce(a2012)) 
+ COUNT(Coalesce(a2013)) + COUNT(Coalesce(a2014)) + 
COUNT(Coalesce(a2015))
),
1
)