Search code examples
azureazure-cosmosdb

How to use WHERE COUNT in Cosmos DB


Is it possible to do a query such as...

SELECT COUNT(UniqueLastNames)
FROM (
SELECT COUNT(c)
FROM c
WHERE COUNT(c.lastName) > 2 AND COUNT(c.lastName) < 5 GROUP BY c.lastName
) AS UniqueLastNames

What it should do is give me all the people whose last name appears more than twice, but less than 5.

Problem is, cosmos won't let you do a function in a WHERE statement it says, any other way around it?


Solution

  • Got it to work with the following code...

    SELECT COUNT(UniqueLastNames)
    FROM (SELECT COUNT(1) AS lastName FROM c GROUP BY c.lastName) AS 
    UniqueLastNames WHERE UniqueEmails.lastName > 2 AND UniqueEmails.lastName < 
    5
    

    ... though it's so expensive on RUs at 137.86 RUs per request.