这篇文章主要介绍“怎么求数据库嵌套表平均值”,在日常操作中,相信很多人在怎么求数据库嵌套表平均值问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操...
这篇文章主要介绍“怎么求数据库嵌套表平均值”,在日常操作中,相信很多人在怎么求数据库嵌套表平均值问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么求数据库嵌套表平均值”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
我创建了一个SCHEMA级别的NUMBER型嵌套表:
CREATE OR REPLACE TYPE plch_numbers_t IS TABLE OF NUMBER
/
哪些选项实现了一个名字为plch_avg的函数,能够计算一个上述嵌套表中所有元素的平均值,从而使得下列代码块执行之后会显示"3.5"?
DECLARE
l_numbers plch_numbers_t
:= plch_numbers_t (1, 2, 3, 4, 5, 6);
BEGIN
DBMS_OUTPUT.put_line (plch_avg (l_numbers));
END;
/
(A)
CREATE OR REPLACE FUNCTION plch_avg (numbers_in IN plch_numbers_t)
RETURN NUMBER
IS
l_index PLS_INTEGER := numbers_in.FIRST;
l_sum NUMBER := 0;
BEGIN
WHILE l_index IS NOT NULL
LOOP
l_sum := l_sum + numbers_in (l_index);
l_index := numbers_in.NEXT (l_index);
END LOOP;
RETURN l_sum / numbers_in.COUNT;
END;
/
(B)
CREATE OR REPLACE FUNCTION plch_avg (numbers_in IN plch_numbers_t)
RETURN NUMBER
IS
l_average NUMBER;
BEGIN
SELECT AVG (COLUMN_VALUE) INTO l_average
FROM TABLE (numbers_in);
RETURN l_average;
END;
/
(C)
CREATE OR REPLACE FUNCTION plch_avg (numbers_in IN plch_numbers_t)
RETURN NUMBER
IS
BEGIN
RETURN AVG (numbers_in);
END;
/
(D)
CREATE OR REPLACE FUNCTION plch_avg (numbers_in IN plch_numbers_t)
RETURN NUMBER
IS
l_index PLS_INTEGER := numbers_in.FIRST;
l_average NUMBER := 0;
BEGIN
WHILE l_index IS NOT NULL
LOOP
l_average := l_average + numbers_in (l_index)/numbers_in.count;
l_index := numbers_in.NEXT (l_index);
END LOOP;
RETURN l_average;
END;
/
(E)
CREATE OR REPLACE FUNCTION plch_avg (numbers_in IN plch_numbers_t)
RETURN NUMBER
IS
l_average NUMBER;
BEGIN
SELECT AVG (numbers_in(ROWNUM)) INTO l_average
FROM numbers_in;
RETURN l_average;
END;
/
实测答案ABC
C报错 PLS-00204: 函数或伪列 'AVG' 只能在 SQL 语句中使用
E报错 PL/SQL: ORA-00942: 表或视图不存在
答案ABD, B是最佳答案,注意D在某些情况下可能出现误差。
到此,关于“怎么求数据库嵌套表平均值”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注捷杰建站网站,小编会继续努力为大家带来更多实用的文章!