栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Oracle/PLSQL: Avg Function

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Oracle/PLSQL: Avg Function

In Oracle/PLSQL, the Avg function returns the average value of an expression.

The basic syntax when using the Avg function is:

SELECt Avg( expression )

FROM tables

WHERe predicates;

expression can be a numeric field or formula.

Apples To

  • Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

Simple Example

For example, you might wish to know how the average salary of all employees whose salary is above $25,000 / year.

SELECt Avg(salary) as "Avg Salary"

FROM employees

WHERe salary > 25000;

Example using DISTINCT

You can use the DISTINCT clause within the Avg function. For example, the SQL statement below returns the average salary of unique salary values where the salary is above $25,000 / year.

SELECt Avg(DISTINCT salary) as "Avg Salary"

FROM employees

WHERe salary > 25000;

If there were two salaries of $30,000/year, only one of these values were be used in the Avg function.

Example using a Formula

The expression contained within the Avg function does not need to be a single field. You could also use a formula. For example, you might want the average commission.

SELECt Avg(sales * 0.10) as "Average Commission"

FROM orders;

Example using GROUP BY

You could also use the Avg function to return the name of the department and the average sales (in the associated department). For example,

SELECt department, Avg(sales) as "Avg sales"

FROM order_details

GROUP BY department;

Because you have listed one column in your SELECT statement that is not encapsulated in the Avg function, you must use a GROUP BY clause. The department field must, therefore, be listed in the GROUP BY section.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1000121.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号