I'm trying to achieve the following query in CosmosDB:
SELECT * FROM c
WHERE c.timestamp = (SELECT VALUE MAX(c.timestamp) FROM c )
However it doesn't seem to be calling the sub query first and returns all rows.
Is this possible?
I'm trying to achieve the following query in CosmosDB:
SELECT * FROM c
WHERE c.timestamp = (SELECT VALUE MAX(c.timestamp) FROM c )
However it doesn't seem to be calling the sub query first and returns all rows.
Is this possible?
I am from CosmosDB Engineering team.
CosmosDB query supports only correlated subqueries, so subqueries can refer to items from the parent collection only. For example, you could utilize aggregates on nested attributes in a document like so:
SELECT TOP 1000
c.id,
MaxNutritionValue,
MinNutritionValue,
AvgNutritionValue
FROM c
JOIN (SELECT VALUE Max(n.nutritionValue) FROM n IN c.nutrients) MaxNutritionValue
JOIN (SELECT VALUE Min(n.nutritionValue) FROM n IN c.nutrients) MinNutritionValue
JOIN (SELECT VALUE Avg(n.nutritionValue) FROM n IN c.nutrients) AvgNutritionValue
assuming a document structure like so:
{
"id":"someId",
"nutrients":[
{
"item": "pizza",
"nutritionValue": 20
},
{
"item": "burger",
"nutritionValue": 30
}
]
}
To achieve what you want, you could do something like this:
SELECT TOP 1 * FROM c ORDER BY c.timestamp DESC
Though not applicable to all aggregates this approach can help with Max or Min.