chhantyal
chhantyal

Reputation: 12252

How to do aggregation (AVG, SUM, MAX etc.) on Azure DocumentDB?

I am trying out Azure DocumentDB and their documentation says aggregation is supported. Indeed, I am able to do COUNT by:

SELECT value COUNT(1) FROM c WHERE c.id = 'someid'

However, if I follow same syntax to do other types of aggregation, it doesn't seem to work.

SELECT value MAX(value) FROM c WHERE c.id = 'someid'

gives error. This is same with MIN.

It looks like aggregation is not supported (even though docs says otherwise). Or is there different approach to do it?

Upvotes: 4

Views: 6219

Answers (1)

David Makogon
David Makogon

Reputation: 71026

For MAX() to work, you need to aggregate on a given property. For example, if you had a few documents with a range property:

{
  "id": "max1",
  "range": 50
}
{
  "id": "max2",
  "range": 20
}

You'd get the max value of range via:

SELECT MAX(c.range) from c

Which returns:

[
  {
    "$1": 50
  }
]

Or

SELECT VALUE MAX(c.range) from c

Which returns:

[
  50
]

Note: In your question's example, you use c.value. VALUE is a reserved word, so you'd need to reference as c['value']. For example:

SELECT VALUE MAX(c['value']) from c

Upvotes: 6

Related Questions