0

以下のスクリプトを使用しているときにこのエラーが発生します。このサイトと Google で多くのスレッドを検索しましたが、問題の解決策が見つかりません。

    select ct.tons as total_tons,
       sum(IF(sum(wr.tons) is not null,sum(wr.tons),0)) as total_delivered,            
       sum(ct.tons - IF(sum(wr.tons) is not null,sum(wr.tons),0)) as total_balance,
             sum(IF(sum(f.tons_fixed) is not null, sum(f.tons_fixed), 0)) as total_fixed,
             sum(IF(sum(f.tons_fixed) is not null, ct.tons - sum(f.tons_fixed), 0)) as total_unfixed,
             avg(sum(IF(f.tons_fixed * f.hedge_price is not null,f.tons_fixed * f.hedge_price, 0))/IF(sum(f.tons_fixed) is not null, sum(f.tons_fixed), 1) + ct.differential_fob) as avg_contract_price,
           avg(db.current_basis) as avg_market_price,            
             avg(ct.contract_price_foreign - db.current_basis) as avg_outright_fob,
             avg(ct.differential_fob) as avg_contract_fob_diff,
             avg(IF(mf.diff is not null, mf.diff, 0)) as avg_market_fob,
             avg(ct.differential_fob - IF(mf.diff is not null, mf.diff, 0)) as avg_diff_fob 
from contract ct 
left join grade_master gm on ct.grade_id = gm.id
left join movement m on m.contract_id = ct.id
left join warehouse_receipt wr on wr.movement_id = m.id
left join daily_basis db on ct.terminal_month = db.id 
left join contract_price_fixation f on f.contract_id = ct.id
left join market_fob_diff mf on mf.grade_id = ct.grade_id
left join grade_master gmt on mf.grade_id = gmt.id
left join company_master cm on cm.id = ct.supplier_buyer_id
GROUP BY ct.id 

左端の合計を削除すると、スクリプトが機能したため、問題は合計内の合計 (avg) である可能性があると思います。それを修正するのを手伝ってください!
ありがとう

4

1 に答える 1

0

このように行を変更します

sum(IF(sum(wr.tons) is not null,sum(wr.tons),0)) as total_delivered

COALESCE(SUM(wr.tons), 0) AS total_delivered

COALESCE()は、null ではない最初のパラメーターを返します。

于 2013-03-27T11:49:48.917 に答える