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
The sample will start with some default options selected. You can immediately click the "Get Statistics" button to see the results for these options. There are several ways to customize your queries:
-
You can add statistic definitions to the top-left table using the combo boxes and "Add" button. Select a table row and click "Remove" to remove a definition.
-
To change the Group-by fields, check the box by the field you want to group by in the bottom-left list view.
-
To change the Order-by fields, select a Group-by field (it must be checked) and click the ">>" button to add it to the Order-by table. To remove a field from the Order-by table, select it and click the "<<" button. To change the sort order of the Order-by field, the cells of the "Sort Order" column are combo-boxes that may be either ASCENDING or DESCENDING.
How it works
- Create a
ServiceFeatureTable
using the URL of a feature service and load the table. - Get the feature tables field names list with
featureTable.getFields()
. - Create
StatisticDefinition
s specifying the field to compute statistics on and theStatisticType
to compute. - Create
StatisticsQueryParameters
passing in the list of statistic definitions. - To have the results grouped by fields, add the field names to the query parameters'
groupByFieldNames
collection. - To have the results ordered by fields, create
OrderBy
s, specifying the field name andSortOrder
. Pass theseOrderBy
s to the parameters'orderByFields
collection. - To execute the query, call
featureTable.queryStatisticsAsync(queryParameters)
. - Get the
StatisticQueryResult
. From this, you can get an iterator ofStatisticRecord
s to loop through and display.
About the data
This sample uses a Diabetes, Obesity, and Inactivity by US County feature layer hosted on ArcGIS Online.
Relevant API
- Field
- QueryParameters.OrderBy
- QueryParameters
- ServiceFeatureTable
- StatisticDefinition
- StatisticRecord
- StatisticsQueryParameters
- StatisticsQueryResult
- StatisticType
Tags
correlation, data, fields, filter, group, sort, statistics, table
Sample Code
package com.esri.samples.statistical_query_group_and_sort;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
/**
* Convenience bean class for representing a group-by field. The grouping property can be bound to a CheckBoxListCell
* to choose whether the field should be grouped by with a CheckBox.
*/
class GroupField {
private final SimpleStringProperty fieldName;
private final SimpleBooleanProperty grouping;
GroupField(String fieldName, Boolean grouping) {
this.fieldName = new SimpleStringProperty(fieldName);
this.grouping = new SimpleBooleanProperty(grouping);
}
String getFieldName() {
return fieldName.get();
}
public SimpleStringProperty fieldNameProperty() {
return fieldName;
}
public void setFieldName(String fieldName) {
this.fieldName.set(fieldName);
}
boolean isGrouping() {
return grouping.get();
}
SimpleBooleanProperty groupingProperty() {
return grouping;
}
void setGrouping(boolean grouping) {
this.grouping.set(grouping);
}
@Override
public String toString() {
return getFieldName();
}
}