簡體   English   中英

優雅地總結 AWS Athena 中的多個列,不包括空值

[英]Gracefully sum multiple columns in AWS Athena excluding nulls

我必須通過在 AWS Athena 中添加一些其他列來創建total_coststotal_proceedstotal_net_loss列。 我還必須用zeros替換nulls ,否則結果將為 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(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)) - (COALESCE(net_sale_proceeds,0) + COALESCE(credit_enhancement_proceeds,0) + COALESCE(repurchase_make_whole_proceeds,0) + COALESCE(other_foreclosure_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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM