0

下面的不同查询现在可以工作:

SELECT DISTINCT c.name  FROM c

但是下面的查询不起作用,这里做错了吗?

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"}
4

1 回答 1

1

SELECT COUNT(DISTINCT c.name) FROM c是 Cosmos DB 不支持的语法错误。

您可以使用存储过程为您的查询结果执行计数任务:

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.');
}
于 2018-10-23T01:21:26.113 回答