azure - Select not in subquery select Cosmos DB,语法错误

标签 azure azure-cosmosdb azure-cosmosdb-sqlapi

我最终会弄清楚,但我想把它放在那里: 此查询:

SELECT distinct value e.siteid
FROM e IN c.events
where e.sensor = 'Air Temperature'

产生这个结果:

[ "07000619231FBD", "07000619236CDB", "09000619236C01", "09000619236BE3", "0A000619236BD7"]

此查询有效,正如预期的那样:

select *
from c
where c.id not in ([ "07000619231FBD", "07000619236CDB", "09000619236C01", "09000619236BE3", "0A000619236BD7"])

但是组合(即子查询)会出现语法错误:

select *
from c
where c.id not in
 (SELECT distinct value e.siteid
  FROM e IN c.events
  where e.sensor = 'Air Temperature')

我在这里缺少什么?

最佳答案

(您的第一个查询也给出了 400 BadREquest。我假设您复制错误,并且实际上在没有 IN c.events 的情况下执行了此操作,因为该查询中未定义 c .)

最有可能的问题是您正在尝试使用跨文档联接进行查询?CosmosDB 不支持联接不同文档。只允许自连接。单独的查询会起作用,因为您可以消除跨文档连接部分。

参见Subquery documentation明确地说(强调我的):

There are two main types of subqueries:

  • Correlated: A subquery that references values from the outer query. The subquery is evaluated once for each row that the outer query processes.
  • Non-correlated: A subquery that's independent of the outer query. It can be run on its own without relying on the outer query.

Note: Azure Cosmos DB supports only correlated subqueries.

您的初衷(单独查询)似乎是非相关查询(跨文档联接),但您实际上编写了一个相关查询(自联接)。您可以做的,就像您已经做的那样,只需连续进行最初的 2 个查询即可。

<小时/>

如果您确实希望子查询仅检查同一文档中的气温(自连接),那么很可能您错过了 ARRAY() 函数:

select *
from c
where c.id not in
 (ARRAY(SELECT distinct value e.siteid
  FROM e IN c.events
  where e.sensor = 'Air Temperature'))

关于azure - Select not in subquery select Cosmos DB,语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63055787/

相关文章:

azure-cosmosdb - DocumentDB - 集合中的 WHERE 子句

azure - 如何为 Azure 上的 Kubernetes 集群中运行的 Grafana 安装自定义插件

azure - 删除 Windows Azure 上 Orchard CMS 的 App_Data

azure - 无法获取 Azure 负载均衡器池的资源 ID

database - 带有 ORDER BY 的 Documentdb SDK 导致 ServiceUnavailableException

azure - 使用数组内容搜索文档在 Cosmos 中不起作用

visual-studio - Visual Studio 中的 Azure Functions 缺少服务总线主题触发器

azure-cosmosdb - 区分 Azure Cosmos DB 中的分区键和分区键范围

python-3.x - Azure 函数 HTTP 触发器 : How to return exception from python worker log to the API caller

azure - 查询 CosmosDb 非结构化 JSON