【问题标题】: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如何编写一个程序,显示 20 岁或以下学生的平均值。在 Oracle SQL Developer 中使用(for 循环)
【发布时间】:2021-01-27 10:25:32
【问题描述】:

基本上我需要在 Oracle SQL Developer 中使用 (for loop) 来显示 20 岁或以下学生的平均值。下面是学生桌

【问题讨论】:

  • 祝你好运!遇到困难时一定要问问题
  • 谢谢!我实际上被困在这个问题上
  • 我猜 HoneyBadger 的意思是说您应该发布您编写的代码,但它并没有按您的意愿工作。
  • 您不需要 FOR 循环来执行此操作。
  • 我确实需要为这个问题使用 for 循环。

标签: sql oracle for-loop oracle-sqldeveloper


【解决方案1】:

我想这是一个家庭作业(简单情况下的有趣要求)。无论如何,看看这样的事情是否有帮助。

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>

【讨论】:

    【解决方案2】:

    试试这个。

    SELECT AVG("AVG") FROM students WHERE age<=20
    

    【讨论】:

    • 它不起作用,而且我需要使用 for 循环
    • 哦,使用for循环是你的要求...让我想想。
    • 尝试使用“AVG”而不是[Avg]
    • 是的,必须使用它
    猜你喜欢
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    • 1970-01-01
    • 2019-05-18
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多