2013-03-27 66 views
0

我在使用下面的腳本時遇到了這個錯誤,我在這個站點和谷歌搜索了很多線程,但是我找不到解決方案。sql錯誤1111-無效的使用組功能

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 

我認爲這個問題可能是求和(平均),因爲當刪除最左邊的總和,腳本工作。請幫我糾正它!
感謝

回答

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()返回第一個它的參數是不是空的。

相關問題