这是我的 Cosmos 数据库中的一个示例项目:
{
"id": "a683-89e01def5310",
"assignmentId": "123",
"auctionSiteId": 100,
"status": "Scheduled",
...
}
当我编写这个基本查询并运行它时没有问题:
SELECT VALUE
COUNT(c.id)
FROM
c
WHERE
c.auctionSiteId = 100 AND c.status = "Scheduled"
我得到了正确的结果。然而,当我让一次性为多个站点计算相同的事情变得有点复杂时,我就不这么认为了。这是我的查询:
with temp as (
select c.auctionSiteId from c
where c.status = "Scheduled"
),
logic as(
select
case when c.auctionSiteId = 96 then 1 else 0 end as count_montreal,
case when c.auctionSiteId = 97 then 1 else 0 end as count_vancouver,
case when c.auctionSiteId = 98 then 1 else 0 end as count_calgary,
case when c.auctionSiteId = 99 then 1 else 0 end as count_edmonton,
case when c.auctionSiteId = 100 then 1 else 0 end as count_toronto,
from temp
)
select
sum(count_montreal) as countMontreal,
sum(count_vancouver) as countVancouver,
sum(count_calgary) as countCalgary,
sum(count_edmonton) as countEdmonton,
sum(count_toronto) as countToronto,
from logic
这是我收到的错误消息:
{"code":"BadRequest","message":"One of the input values is invalid.\r\nActivityId: 5a1a98e3-aca1-40b4-8378-6cd5b4ba2bbf, Wind...
SELECT
SUM(c.auctionSiteId = 13 ?1:0) AS countMontreal,
SUM(c.auctionSiteId = 15 ?1:0) AS countVancouver,
SUM(c.auctionSiteId = 35 ?1:0) AS countCalgary,
SUM(c.auctionSiteId = 54 ?1:0) AS countEdmonton,
SUM(c.auctionSiteId = 60 ?1:0) AS countToronto
FROM c
WHERE c.status = "Scheduled"
希望这有帮助。 Cosmos 与 SQL 不同,不支持 case when 语法。建议使用 三元运算符 代替