我有我的軌道與postgre數據庫應用程序,我試圖得到一些指標,用下面的語句Rails的:防止被零除在Postgres
Negotiation.find(:all, :conditions => conditions_hash('negotiation'), :select => 'sum(payment_term * amount_after_annualised_base)/sum(amount_after_annualised_base) as pt').first
我怎麼能確保總和(amount_after_annualised_base )不是0(因此不會拋出「除零」錯誤)?
我發現了這個,但不知道它是正確的/最好的/最有效的方式。
Negotiation.find(:all, :conditions => conditions_hash('negotiation'), :select => 'sum(payment_term * amount_after_annualised_base)/(CASE sum(amount_after_annualised_base) WHEN 0 THEN NULL ELSE sum(amount_after_annualised_base) END) as pt').first
謝謝! 皮埃爾
這是行不通的?因爲你仍然試圖在所有情況下進行分工。 – nathanvda 2011-03-01 14:12:36
它實際上工作。爲了簡明起見,我拿這個 – Pierre 2011-03-01 18:52:36