# Avg - 脚本函数

Avg() 用于查找迭代于 group by 子句定义的大量记录的表达式中聚合数据的平均值。

语法：

Avg([DISTINCT] expr)

返回数据类型： 数字

参数：

参数

| 参数 | 说明 |
| --- | --- |
| expr | 表达式或字段包含要度量的数据。 |
| DISTINCT | 如果在表达式前面出现单词 distinct，则将忽略所有重复值。 |

示例和结果：

将示例脚本添加到应用程序并运行。要查看结果，将结果列中列出的字段添加到应用程序中的工作表。

结果数据

| 示例 | 结果 |
| --- | --- |
| `Temp:`<br>`crosstable (Month, Sales) load * inline [`<br>`Customer\|Jan\|Feb\|Mar\|\|Apr\|May\|Jun\|Jul\|Aug\|Sep\|Oct\|Nov\|Dec`<br>`Astrida\|46\|60\|70\|13\|78\|20\|45\|65\|78\|12\|78\|22`<br>`Betacab\|65\|56\|22\|79\|12\|56\|45\|24\|32\|78\|55\|15`<br>`Canutility\|77\|68\|34\|91\|24\|68\|57\|36\|44\|90\|67\|27`<br>`Divadip\|36\|44\|90\|67\|27\|57\|68\|47\|90\|80\|94`<br>`] (delimiter is '\|');`<br><br>`Avg1:` `LOAD Customer, Avg(Sales) as MyAverageSalesByCustomer Resident Temp Group By Customer;<br>` ` ` | `Customer MyAverageSalesByCustomer` `Astrida 48.916667` `Betacab 44.916667` `Canutility 56.916667` `Divadip 63.083333`这可以通过创建包括以下度量的表格在工作表中进行检查：<br>`Sum(Sales)/12` |
| 前提是 Temp 表格像之前的示例一样加载：<br><br>`LOAD  Customer,Avg(DISTINCT Sales) as MyAvgSalesDistinct Resident Temp Group By Customer;` | `Customer MyAverageSalesByCustomer` `Astrida 43.1` `Betacab 43.909091` `Canutility 55.909091` `Divadip 61`只会对特殊值进行计数。用总数除以非重复值的个数。 |