简体   繁体   中英

Using sum function in sql view in oracle database

I am trying to create a view using some o group function like sum , avg etc. it is giving error.

create view sample_view as 
select A,B,C,D,E, 
       (a + b+c+d+e)/5 as Mean_value, 
       GREATEST(a,b,c,d,e)-LEAST(a,b,c,d,e) as range_value, 
       avg(mean_value)  
from samples;

I am getting below error.

Error starting at line : 37 in command -
create view sample_view as select A,B,C,D,E, (a + b+c+d+e)/5 as Mean_value, GREATEST(a,b,c,d,e)-LEAST(a,b,c,d,e) as range_value, avg(mean_value)  from samples
Error report -
ORA-00937: not a single-group group function
00937. 00000 -  "not a single-group group function"
*Cause:    
*Action:

Is there anyway to create view in oracle database using group function. I also tried sum() and count(). all those also giving error.

问题的根本原因是:您尝试执行包含 GROUP BY 函数(即:AVG 函数、MIN 函数、MAX 函数、SUM 函数、COUNT 函数)的SELECT语句,但缺少GROUP BY 子句。尝试添加Group BY 子句并运行查询。

You can try the below - since avg() is an aggregate function you can not add this with other columns without adding group by the clause -

create view sample_view as 
       select A,B,C,D,E, (a + b+c+d+e)/5 as Mean_value, 
              GREATEST(a,b,c,d,e)-LEAST(a,b,c,d,e) as range_value, 
              avg((a + b+c+d+e)/5) over() 
from samples;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM