Oracle分組查詢中包含子查詢列,發生ORA-00937:不是單分組函式的錯誤

notis發表於2024-04-05
 select sum(raw_ore) raw_ore
 ,(select sum(raw_ore0) from trv_daily where p_year=a.p_year ) lift_ore   
  from trv_refine a  where p_year=2024

儘管語句select sum(raw_ore0) from trv_daily where p_year=a.p_year ,只返回一個數值,系統提示“ORA-00937:不是單分組函式”

將子查詢外面新增max等統計函式,即可解決該問題。

 select sum(raw_ore) raw_ore
 ,max((select sum(raw_ore0) from trv_daily where p_year=a.p_year )) lift_ore   
  from trv_refine a  where p_year=2024

總結:在分組查詢中,只允許2類列,分組依據列(group by)和分組查詢列(sum、count)等。

相關文章