Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

How do I write a program where it shows the average of the average for students whose age are 20 or below. Using (for loop) in Oracle SQL Developer

Basically I need to use (for loop) in Oracle SQL Developer to show the average of the average for students whose age is 20 or below. Down Below is the students' table

The Table

question from:https://stackoverflow.com/questions/65917179/how-do-i-write-a-program-where-it-shows-the-average-of-the-average-for-students

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I guess it is a homework (funny requirements for simple situations). Anyway, see if something like this helps.

SQL> set serveroutput on
SQL>
SQL> declare
  2    l_avg number;
  3    l_sum number := 0;
  4    l_cnt number := 0;
  5  begin
  6    for cur_r in (select avg from test where age <= 20) loop
  7      l_cnt := l_cnt + 1;
  8      l_sum := l_sum + cur_r.avg;
  9    end loop;
 10
 11    if l_cnt > 0 then
 12       l_avg := l_sum / l_cnt;
 13    end if;
 14
 15    dbms_output.put_line('Average is ' || nvl(to_char(l_avg), 'unknown'));
 16  end;
 17  /
Average is 68

PL/SQL procedure successfully completed.

SQL>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...