我想在 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
列?
也许有更简洁的方法来做到这一点,但我通常会这样做:
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