【发布时间】:2015-11-24 11:18:00
【问题描述】:
我有一个包含以下字段的表格:country_code、short_name、currency_unit、a2010、a2011、a2012、a2013、a2014、a2015。 a2010-a2015 字段是双精度类型。
如何进行查询,按字段 a2010-a2015 的平均值对结果进行排序,记住这些字段可能具有 NULL 值?
我尝试了这段代码,但它不起作用(返回一个错误,这表明 ORDER BY 部分有问题。错误是关于 coumn 名称和 GROUP BY)。逻辑是:ORDER BY ((A)/(B)) 其中 A - 非 NULL 字段的总和 B - 非 NULL 字段的计数。
有什么想法吗?
(如果重要,代码将在 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;
【问题讨论】:
标签: mysql db2 bigdata biginsights bigsql