微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

优雅地对AWS Athena中的多个列求和,但不包括空值

如何解决优雅地对AWS Athena中的多个列求和,但不包括空值

我必须通过在AWS Athena中添加其他几列来创建total_coststotal_proceedstotal_net_loss列。我还必须将nulls替换为zeros,否则结果将为null。我尝试使用sum()函数,但仅在单列上起作用。以下查询完成了我想做的事情,但是看起来很不优雅。还有其他更优雅的方法来汇总Athena中的列吗?

SELECT loan_identifier,monthly_reporting_period,current_actual_upb,current_loan_delinquency_status,(COALESCE(foreclosure_costs,0) + COALESCE(property_preservation_and_repair_costs,0) + COALESCE(asset_recovery_costs,0) + COALESCE(miscellaneous_holding_expenses_and_credits,0) + COALESCE(associated_taxes_for_holding_property,0)) AS total_costs,(COALESCE(net_sale_proceeds,0) + COALESCE(credit_enhancement_proceeds,0) + COALESCE(repurchase_make_whole_proceeds,0) + COALESCE(other_foreclosure_proceeds,0)) AS total_proceeds,current_actual_upb + (current_actual_upb * ((current_interest_rate/100 - 0.0035)/12)*DATE_DIFF('day',last_paid_installement_date,disposition_date)/30) + (COALESCE(foreclosure_costs,0)) - (COALESCE(net_sale_proceeds,0)) AS total_net_loss
FROM performance_data;

解决方法

结合使用SUMCOALESCE,如下所示:

SELECT SUM(COALESCE(y,0) + COALESCE(z,0)) AS aggregate_sum_of_y_and_z
FROM (
  VALUES (1,2),(NULL,3),(2,1)) AS x (y,z)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。