Skip To Content ArcGIS for Developers Sign In Dashboard

Get feature statistics


Follow these steps to query a feature table for statistical information:

  1. Load a feature table that supports statistical queries from a local or online data source. This may be a layer shown in a map view in your app, or a table you’ve loaded but not displayed.

    // create the US states feature table from the US states map service
    self.statesTable = AGSServiceFeatureTable(url: URL(string: "")!)
    // load the service feature table
    self.statesTable.load { (error) in
        if let error = error {
        // ...

  2. Define statistics to return from the query. Create an AGSStatisticDefinition for each statistic you want to query from the table. This includes the field name, statistic type, and output alias name for the result.

    // create each statistic definition for the query
    let statDefAvgPop = AGSStatisticDefinition(onFieldName: "POP2007", statisticType: .average, outputAlias: "AveragePop")
    let statDefSumPop = AGSStatisticDefinition(onFieldName: "POP2007", statisticType: .sum, outputAlias: "TotalPop")
    let statDefCount = AGSStatisticDefinition(onFieldName: "OBJECTID", statisticType: .count, outputAlias: "StateCount")

  3. Create an AGSStatisticsQueryParameters object, passing in your list of (one or more) statistic definitions.

    // create a list of the definitions
    let statisticDefinitions = [statDefAvgPop, statDefSumPop, statDefCount]
    // create a new StatisticsQueryParameters object, pass in the list of definitions
    let statisticsQueryParams = AGSStatisticsQueryParameters(statisticDefinitions: statisticDefinitions)

  4. Define spatial, attribute, or temporal filters (if any) and add them to the query parameters.

    // filter the query to only include features in an area of interest
    statisticsQueryParams.geometry = areaOfInterestPolygon
    statisticsQueryParams.spatialRelationship = .intersects

  5. Specify the fields you want to group and order the results with.

    // use the SUB_REGION field to group results
    // create an OrderBy object to sort on the SUB_REGION field in ascending order
    statisticsQueryParams.orderByFields = [AGSOrderBy(fieldName: "SUB_REGION", sortOrder: .ascending)]

  6. Execute the query. The results are returned as an AGSStatisticsQueryResult.

    // execute the statistical query with these parameters and await the results
    self.statesTable.queryStatistics(with: statisticsQueryParams) { (result, error) in
        if let error = error {
        guard let statisticResult = result else {return}
        //process the result

  7. Iterate the result records to read the statistic values. The output alias name defined for each statistic definitions is the key for a corresponding value. If results were grouped, iterate the group names and associated collection of values.

    self.statesTable?.queryStatistics(with: statisticsQueryParams) { (result, error) in
        if let error = error {
        guard let statisticResult = result else {return}
        let statisticRecordEnumerator = statisticResult.statisticRecordEnumerator()
        var statsWithoutGroup = [""]
        while statisticRecordEnumerator.hasNextObject() {
            let statisticRecord = statisticRecordEnumerator.nextObject()
            if let statisticRecord = statisticRecord {
                // if statistic record contains no grouping
                if {
                    for (key, value) in statisticRecord.statistics  {
                        statsWithoutGroup.append("\(key): \(value)")
                else {
                    // get group for each statistic record
                    for group in {
                        //iterate the group to get the statistics

Tables that support statistical queries

To see if a feature table supports a statistical query, check AGSArcGISFeatureLayerInfo.supportsStatistics for a feature layer or AGSArcGISMapServiceSublayerInfo.supportsStatistics for an ArcGIS map image sublayer.


If the query capability is disabled in the service capabilities, then statistics query is also disabled.

When using a service feature table (reading data from an online service, in other words), statistical queries are executed on the service unless the table is in manual cache mode, in which case the query executes on the locally available (cached) data. All other feature tables also execute statistical queries against a local data source.

Define a statistics query

Use an AGSStatisticsQueryParameters to define:

  • Statistics to calculate and their output alias field names
  • How results are grouped and ordered
  • Filters that restrict which features are evaluated

Each of these are described in the following sections.

Statistic definitions

An AGSStatisticDefinition specifies the name of the field that contains values to query, the statistic type, and the output (alias) name to use for the results. A statistics query must contain at least one statistic definition, but may contain several. A field of any data type can be used for statistic types Count, Minimum, or Maximum, which will return the number of values, first value, or last value (when sorted in ascending order) respectively. Other statistic types: Average, Sum, Standard Deviation, and Variance are only valid for numeric fields. If these statistics are attempted on a non-numeric field, you will receive an error from the server ("unable to complete operation").

If the output statistic alias name is empty or missing, a default name will be assigned by the map server for the returned statistic field. The server-generated output name is composed of the field name and the statistic type separated by an underscore. If you specify a name, it can only contain alphanumeric characters and an underscore. If the output name is a reserved keyword of the underlying DBMS, the operation may fail.

Group and order results

Unless otherwise specified, the results from a statistical query will not be grouped using a table attribute. In this case, the statistic values returned are single values that describe all the input features. If one or more group fields are specified, the results contain a value for each statistic for each group. If getting average population for world cities grouped by countries, for example, each country would have a value for the average population of their cities. Without this grouping, you would get a single value for all cities in the world.

Results are also not ordered unless you specify one or several fields to sort on. The fields you choose to order results by must be fields you've also chosen for grouping. The following example shows results for statistics on US states grouped by sub-region. The results are also sorted by sub-region (in ascending order).

Statistics query results grouped and ordered by sub-region.

Filter features to evaluate

You can define any combination of spatial, attribute, or temporal criteria to filter the features that are evaluated by the statistical query. If none of these filters are set, the query will be evaluated against all features in the table.

  • Attribute—define a where expression that uses any of the fields in the table.
  • Spatial—use a geometry object and a spatial relationship.
  • Temporal—specify a time or time range.

Process results from a statistical query

The results from a statistical query are returned as an enumerator of AGSStatisticRecord objects. Each record describes a group and contains a dictionary of group values and a dictionary of statistic values. The group dictionary contains key/value pairs that describe each group field, with the field name ("SUB_REGION", for example) as the key for the field value ("Mountain", for example). Since multiple group fields can be specified for a query, each record in the results represents a unique combination of the group field values.

The statistics dictionary for each record contains the requested statistics for the group. The key for each statistic value is the alias name (by default, in the form fieldName_statisticType). If no group fields were specified for the query, the groups dictionary is empty and a single set of statistics are returned to describe all features included in the query.