cosmos DB GROUP BY 查询中的多个 COUNT 列

问题描述 投票:0回答:1

我想在 cosmos DB

COUNT
查询中使用多个
GROUP BY
列。我尝试过类似这样的查询。

SELECT c.departmentName,
CASE WHEN c.age > 50 THEN COUNT(c.EmpId) ELSE 0 END AS category1,
CASE WHEN c.age <= 49 THEN COUNT(c.EmpId) ELSE 0 END AS category2
FROM c
GROUP BY c.departmentName

但是我收到错误消息:

“语法错误,‘CASE’附近的语法不正确。”

如何通过过滤属性来应用多个

COUNT
列?

sql sql-server azure-cosmosdb azure-cosmosdb-sqlapi
1个回答
0
投票

也许有更简洁的方法来做到这一点,但我通常会这样做:

SELECT c.departmentName,
CASE WHEN c.age > 50 and c.EmpId is not null 
          THEN SUM(1) END AS category1,
CASE WHEN c.age <= 49 and c.EmpId is not null 
          THEN SUM(1) END AS category2
FROM c
GROUP BY c.departmentName
© www.soinside.com 2019 - 2024. All rights reserved.