Query a feature table for statistics, grouping and sorting by different fields.

Use case
You can use statistical queries, grouping, and sorting to process large amounts of data saved in feature tables. This is helpful for identifying trends and relationships within the data, which can be used to support further interpretations and decisions. For example, a health agency can use information on medical conditions occurring throughout a country to identify at-risk areas or demographics, and decide on further action and preventive measures.
How to use the sample
Select a combination of fields and statistic types to include in the query. Choose one or more fields by which to group the results. For example, selecting “State” will calculate the results by state. Choose one or more fields to order results by. Only those fields selected for grouping are valid choices for ordering results. Tap “Edit” to rearrange and delete the fields and statistic types. Tap the “Query Statistics” button to execute the query. Results will be displayed in a hierarchical view that is grouped and sorted according to the chosen fields. Tap “Reset” to clear the fields and statistic types.
How it works
- Create a
ServiceFeatureTableusing the URL of a feature service and load the table. - Create
StatisticDefinitionobjects and use them to createStatisticsQueryParameters. - To have the results grouped by fields, add the field names to the query parameters’
groupByFieldNamesarray. - To have the results ordered by fields, create
OrderByobjects, specifying the field name andSortOrder. Add theseOrderBys to the parameters’orderByFieldscollection. - To execute the query, call
FeatureTable.queryStatistics(using:). - From the
StatisticQueryResult, you can useStatisticsQueryResult.statisticRecords()to loop through and display theStatisticRecordobjects.
Relevant API
- Field
- OrderBy
- QueryParameters
- ServiceFeatureTable
- StatisticDefinition
- StatisticRecord
- StatisticsQueryParameters
- StatisticsQueryResult
- StatisticType
About the data
This sample uses a Diabetes, Obesity, and Inactivity by US County feature layer hosted on ArcGIS Online.
Tags
correlation, data, fields, filter, group, sort, statistics, table