0

The below distinct query now works:

SELECT DISTINCT c.name  FROM c

but the below query is not working, is there something done wrong here?

SELECT COUNT(DISTINCT c.name) FROM c

Failed to query documents for collection products: {"code":400,"body":"{\"code\":\"BadRequest\",\"message\":\"Message: {\\\"errors\\\":[{\\\"severity\\\":\\\"Error\\\",\\\"location\\\":{\\\"start\\\":13,\\\"end\\\":21},\\\"code\\\":\\\"SC1001\\\",\\\"message\\\":\\\"Syntax error, incorrect syntax near 'DISTINCT'.\\\"}]}\\r\\nActivityId: 05bf1757-ce1b-4ee4-9ae5-f2b778c55ded, Microsoft.Azure.Documents.Common/2.0.0.0\"}","activityId":"05bf1757-ce1b-4ee4-9ae5-f2b778c55ded"}
David Makogon
  • 69,407
  • 21
  • 141
  • 189
Santosh
  • 325
  • 5
  • 19

1 Answers1

1

SELECT COUNT(DISTINCT c.name) FROM c is syntax error which is not supported by Cosmos DB.

You could use Stored Procedure to do the count task for your query results:

function sample() {
    var collection = getContext().getCollection();
    var isAccepted = collection.queryDocuments(
        collection.getSelfLink(),
        'SELECT distinct r.name FROM root r',
    function (err, feed, options) {
        if (err) throw err;

        if (!feed || !feed.length) {
            var response = getContext().getResponse();
            response.setBody('no docs found');
            //or return 0 
        }
        else {
            var response = getContext().getResponse();
            response.setBody(feed.length);
        }
    });

    if (!isAccepted) throw new Error('The query was not accepted by the server.');
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Jay Gong
  • 23,163
  • 2
  • 27
  • 32