Tuesday, November 12, 2019

GROUP BY support in Azure CosmosDB


      Microsoft announced the GROUP BY clause support for Azure Cosmos DB at Ignite 2019. Probably, this was one of the most requested feature by users. As you might know, Aggregation happens in client side in Cosmos DB not in the database engine. Azure Cosmos DB has many client-side SDKs and GROUP BY is supported by in .NET SDK 3.3 and above, JavaScript SDK 3.4 and above. I am sure other language SDK will support GROUP BY in future. Rather than SDK, you can use GROUP BY in SQL API queries.

     Another limitation of GROUP BY in Cosmos DB is; you cannot use it with an ORDER BY clause. GROUP BY clause can be used after WHERE clause and before the OFFSET LIMIT clause.
REST API do not support GROUP BY either, you need to aggregate the data manually if you want to use GROUP BY.

     Here is an example using GROUP by in Azure Portal. Following example shows type of posts and count of each post type for a specific user.


3 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. Could you please click the QueryStats tab and share the screenshot ? I am curious about the request unit of a simple group by query ;)

    ReplyDelete