# Class AggregationQuery (3.1.0)

3.1.0 (latest) 2.40.0 2.37.0 2.36.0 2.34.0 2.33.3 2.32.3 2.31.4 2.30.0 2.29.1 2.28.2 2.27.1 2.26.4 2.25.2 2.24.3 2.23.0 2.22.0 2.21.3 2.20.2 2.19.2 2.18.5 2.17.6

    public class AggregationQuery extends Query<AggregationResults>

An implementation of a Google Cloud Datastore Query that returns [AggregationResults](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.AggregationResults), It
can be constructed by providing a nested query ([StructuredQuery](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.StructuredQuery) or [GqlQuery](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.GqlQuery)) to
run the aggregations on and a set of [Aggregation](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.aggregation.Aggregation).

[StructuredQuery](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.StructuredQuery) example:


     EntityQuery selectAllQuery = Query.newEntityQueryBuilder()
        .setKind("Task")
        .build();
     AggregationQuery aggregationQuery = Query.newAggregationQueryBuilder()
        .addAggregation(count().as("total_count"))
        .over(selectAllQuery)
        .build();
     AggregationResults aggregationResults = datastore.runAggregation(aggregationQuery);
     for (AggregationResult aggregationResult : aggregationResults) {
         System.out.println(aggregationResult.get("total_count"));
     }
     
[GqlQuery](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.GqlQuery) example:


     GqlQuery selectAllGqlQuery = Query.newGqlQueryBuilder(
             "AGGREGATE COUNT(*) AS total_count, COUNT_UP_TO(100) AS count_upto_100 OVER(SELECT * FROM Task)"
         )
         .setAllowLiteral(true)
         .build();
     AggregationQuery aggregationQuery = Query.newAggregationQueryBuilder()
         .over(selectAllGqlQuery)
         .build();
     AggregationResults aggregationResults = datastore.runAggregation(aggregationQuery);
     for (AggregationResult aggregationResult : aggregationResults) {
       System.out.println(aggregationResult.get("total_count"));
       System.out.println(aggregationResult.get("count_upto_100"));
     }
     
See Also: [Datastore queries](https://cloud.google.com/appengine/docs/java/datastore/queries)

## Inheritance

[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \> [Query](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.Query) \> AggregationQuery

## Inherited Members

[Query.\<V\>newGqlQueryBuilder(Query.ResultType\<V\>,String)](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.Query#com_google_cloud_datastore_Query__V_newGqlQueryBuilder_com_google_cloud_datastore_Query_ResultType_V__java_lang_String_) [Query.getNamespace()](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.Query#com_google_cloud_datastore_Query_getNamespace__) [Query.newAggregationQueryBuilder()](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.Query#com_google_cloud_datastore_Query_newAggregationQueryBuilder__) [Query.newEntityQueryBuilder()](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.Query#com_google_cloud_datastore_Query_newEntityQueryBuilder__) [Query.newGqlQueryBuilder(String)](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.Query#com_google_cloud_datastore_Query_newGqlQueryBuilder_java_lang_String_) [Query.newKeyQueryBuilder()](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.Query#com_google_cloud_datastore_Query_newKeyQueryBuilder__) [Query.newProjectionEntityQueryBuilder()](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.Query#com_google_cloud_datastore_Query_newProjectionEntityQueryBuilder__) [Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) [Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) [Object.finalize()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--) [Object.getClass()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--) [Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) [Object.notify()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) [Object.notifyAll()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--) [Object.toString()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) [Object.wait()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) [Object.wait(long)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-) [Object.wait(long,int)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-)

## Methods

### getAggregations()

    public Set<Aggregation> getAggregations()

Returns the [Aggregation](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.aggregation.Aggregation)(s) for this Query.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/util/Set.html<https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.aggregation.Aggregation>` |   |

### getMode()

    public AggregationQuery.Mode getMode()

Returns the Mode for this query.

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.AggregationQuery.Mode` |   |

### getNestedGqlQuery()

    public GqlQuery<?> getNestedGqlQuery()

Returns the underlying [for this Query](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.GqlQuery). Returns null if created with [StructuredQuery](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.StructuredQuery)

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.GqlQuery<?>` |   |

### getNestedStructuredQuery()

    public StructuredQuery<?> getNestedStructuredQuery()

Returns the underlying [for this Query](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.StructuredQuery). Returns null if created with
[GqlQuery](https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.GqlQuery)

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-datastore/latest/com.google.cloud.datastore.StructuredQuery<?>` |   |