'Azure CosmosDB, count with subquery

I'm currently using CosmosDB to store some reviews data. I'm trying to retrieve the average rating from a specific date and also the number of registers that have a rating below 3.

The query that I'm using to retrieve the average rating and count the number of registers with ratings below 3 are:

**Ratings**
SELECT avg(c.x_review) as avg_x_review,
        avg(c.y_review) as avg_y_review,
        {date} as date
FROM c where c.date = {date}
**Criticals (below 3)**
SELECT count(1) FROM c where c.date = {date}
AND (
        c.x_review < 3 OR
        c.y_review < 3 
    )

I wanted it to be inside just one query. This data is going to be retrieved by an Azure Function HTTP Trigger, I will use and save the generated JSON from this query in another container.

I've been searching for tips but nothing seems to work, one example of a query that I'm trying to reach is:

  SELECT avg(c.x_review) as avg_x_review,
         avg(c.y_review) as avg_y_review,
         count(SELECT * FROM c where c.date = {date}
                AND (
                      c.x_review < 3 OR
                      c.y_review < 3 
                    )
              ) as criticals
    FROM c where c.date = {date}

...or something like that where I could have both queries inside one query alone.

I'm expecting to generate a JSON like this:

{
    "avg_x_review": 5,
    "avg_y_review": 4,
    [...]
    [...]
    "criticals": 0,
    "date": "2022-02-28"
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source