ORA-00937:不是單組分組函式 ORA-22818:這裡不允許出現子查詢表示式
今天,寫了以下SQL語句:
select (select well_name from well where well_id = wa.well_id) well_name,sum(prod_time)/24
prod_days
FROM pro_well_vol_daily daily, well_attribute wa
WHERE daily.well_id = wa.well_id
報錯:ORA-00937:不是單組分組函式
加上GROUP BY之後,是這樣的:
select (select well_name from well where well_id = wa.well_id) well_name,sum(prod_time)/24
prod_days
FROM pro_well_vol_daily daily, well_attribute wa
WHERE daily.well_id = wa.well_id
group by (select well_name from well where well_id = wa.well_id)
但是,報錯:ORA-22818:這裡不允許出現子查詢表示式。
在群裡問了一下,那個高手說可以吧well那個表放在外面進行查詢。
於是,我試了一下:
SELECT w.well_name ,sum(prod_time)/24 prod_days
FROM pro_well_vol_daily daily, well_attribute wa, well w
WHERE daily.well_id = wa.well_id
AND well_id = wa.well_id
group by w.well_name;
可以~\(≧▽≦)/~啦啦啦。
還有一種方式:
當查詢語句中有MAX,AVG等聚合函式,SELECT 後面出現的欄位或子查詢必須出現在GROUP BY後面(group
by後面不能跟子查詢)。
但若不想按這個欄位排序,又想讓這個欄位出現在查詢語句中,可以在這些欄位或子查詢前加
MAX,MIN,DECODE等函式。
select MAX( (select well_name from well where well_id = wa.well_id) )well_name,sum(prod_time)/24
prod_days
FROM pro_well_vol_daily daily, well_attribute wa
WHERE daily.well_id = wa.well_id