gpt4 book ai didi

google-bigquery - 避免 BigQuery 中的相关子查询错误

转载 作者:行者123 更新时间:2023-12-04 06:31:55 25 4
gpt4 key购买 nike

我有一个简单的查询来获取创建交易时使用的货币汇率:

SELECT t.orderid, t.date, 
(SELECT rate FROM sources.currency_rates r WHERE currencyid=1 AND
r.date>=t.date ORDER BY date LIMIT 1) rate
FROM sources.transactions t

这会触发一个错误:
Error: Correlated subqueries that reference other tables are not 
supported unless they can be de-correlated, such as by transforming
them into an efficient JOIN.'

我尝试了几种类型的连接和命名子查询,但似乎都不起作用。实现这一目标的最佳方法是什么?似乎是一个非常常见的场景,在 BQ 的标准 Sql 中实现起来应该非常简单。

最佳答案

下面是 BigQuery 标准 SQL



#standardSQL
SELECT
t.orderid AS orderid,
t.date AS date,
ARRAY_AGG(r.rate ORDER BY r.date LIMIT 1)[SAFE_OFFSET(0)] AS rate
FROM `sources.transactions` AS t
JOIN `sources.currency_rates` AS r
ON currencyid = 1
AND r.date >= t.date
GROUP BY orderid, date

关于google-bigquery - 避免 BigQuery 中的相关子查询错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45040472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com