FM.SQL.Avg
----------

Calculates average value in a record set.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [FM](component_FM.md) [FMSQL](component_FMSQL.md) | [9.1](newinversion91.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "FM.SQL.Avg"; SQLref; Column { ; FirstRow; LastRow } ) 

**MBS**( **"FM.SQL.Avg";** /\* Calculates average value in a record set. \*/  
**$SQLref**; /\* The reference number returned by [FM.SQL.Execute](FMSQLExecute.md) function. \*/   
**$Column**; /\* The index of the column.  
Default is 0.e.g. 0 \*/   
**$FirstRow**; /\* Optional; The index of first row.  
Default is 0.e.g. 0 \*/   
**$LastRow**) /\* Optional; The index of last row.  
Default is [FM.SQL.RowCount](FMSQLRowCount.md)-1.e.g. 5 \*/ 

### Parameters

| Parameter | Description | Example | Flags |
|---|---|---|---|
| SQLref | The reference number returned by [FM.SQL.Execute](FMSQLExecute.md) function. | $SQLRef |  |
| Column | The index of the column.   Default is 0. | 0 |  |
| FirstRow | The index of first row.   Default is 0. | 0 | Optional |
| LastRow | The index of last row.   Default is [FM.SQL.RowCount](FMSQLRowCount.md)-1. | 5 | Optional |

### Result

Returns number or error.

### Description

Calculates average value in a record set.  
You got a record set in memory and we can walk over a column in a given row range to add the sum value and later divide by number of values.  
### Examples

Query once and make several calculations:

 # let FileMaker query data once   
Set Variable \[ $Records ; Value: MBS ( "[FM.SQL.Execute](FMSQLExecute.md)"; Get(FileName); "SELECT Price FROM Produkte") \]   
\# Now get sum, min, max and avg   
Set Variable \[ $sum ; Value: MBS ( "[FM.SQL.Sum](FMSQLSum.md)"; $records ; 0) \]   
Set Variable \[ $min ; Value: MBS ( "[FM.SQL.Min](FMSQLMin.md)"; $records ; 0) \]   
Set Variable \[ $max ; Value: MBS ( "[FM.SQL.Max](FMSQLMax.md)"; $records ; 0) \]   
Set Variable \[ $avg ; Value: MBS ( "FM.SQL.Avg"; $records ; 0) \]   
Set Variable \[ $text ; Value: MBS ( "[FM.SQL.Text](FMSQLText.md)"; $records ; "" /\* firstRow \*/; "" /\* lastRow \*/; "" /\* firstCol \*/; "" /\* lastCol \*/; ", " /\* rowSep \*/) \]   
Set Variable \[ $r ; Value: MBS ( "[FM.SQL.Release](FMSQLRelease.md)"; $records ) \]   
Show Custom Dialog \[ "Sum: " &amp; $sum &amp; ¶ &amp; "Min: " &amp; $min &amp; ¶ &amp; "Max: " &amp; $max &amp; ¶ &amp; "Avg: " &amp; $avg &amp; ¶ &amp; "text: " &amp; $text \]  
### See also

- [FM.SQL.Execute](FMSQLExecute.md)
- [FM.SQL.Max](FMSQLMax.md)
- [FM.SQL.Min](FMSQLMin.md)
- [FM.SQL.Release](FMSQLRelease.md)
- [FM.SQL.RowCount](FMSQLRowCount.md)
- [FM.SQL.Sum](FMSQLSum.md)
- [FM.SQL.Text](FMSQLText.md)
- [SQL.Execute](SQLExecute.md)

### Release notes

- **Version 9.1**
    - Added [FM.SQL.Max](http://www.mbsplugins.eu/FMSQLMax.shtml), [FM.SQL.Min](http://www.mbsplugins.eu/FMSQLMin.shtml), [FM.SQL.Avg](http://www.mbsplugins.eu/FMSQLAvg.shtml) and [FM.SQL.Sum](http://www.mbsplugins.eu/FMSQLSum.shtml) functions.

### Blog Entries

- [MBS FileMaker Advent calendar - Door 9 - Totals and averages](https://www.mbsplugins.de/archive/2025-12-09/MBS_FileMaker_Advent_calendar_/monkeybreadsoftware_blog_filemaker)
- [Neues MBS FileMaker Plugin 9.1](https://www.mbsplugins.de/archive/2019-03-12/Neues_MBS_FileMaker_Plugin_91/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin 9.1 - More than 5700 Functions In One Plugin](https://www.mbsplugins.de/archive/2019-03-12/MBS_FileMaker_Plugin_91_-_More/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 9.1pr1](https://www.mbsplugins.de/archive/2019-02-06/MBS_FileMaker_Plugin_version_9/monkeybreadsoftware_blog_filemaker)
- [FileMaker SQL with Sum, Min, Max and Avg on results](https://www.mbsplugins.de/archive/2019-01-30/FileMaker_SQL_with_Sum_Min_Max/monkeybreadsoftware_blog_filemaker)

This function checks for a license.

Created 30th January 2019 , last changed 26th February 2021

  
[FM.RunScriptLater](FMRunScriptLater.md) - [FM.SQL.CSV](FMSQLCSV.md)

[HTML Version](FMSQLAvg.shtml)