我有一个简单的查询来获取创建交易时使用的货币汇率:
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中实现起来非常简单。
答案 0 :(得分:5)
以下是BigQuery Standard SQL
String oldstring = "2011-01-18 00:00:00";
LocalDateTime datetime = LocalDateTime.parse(oldstring, DateTimeFormatter.ofPattern("YYYY-MM-DD HH:MM:SS"));
答案 1 :(得分:3)
我注意到与其他相关子查询的类似行为。它们很有用,但不能总是通过BigQuery自动建模为JOIN。
类似的案例有效:
#standardSQL
SELECT name, (
SELECT AVG(temp)
FROM `bigquery-public-data.noaa_gsod.gsod2017` b
WHERE a.usaf=b.stn
) temp
FROM `bigquery-public-data.noaa_gsod.stations` a
LIMIT 10
不能工作:
#standardSQL
SELECT name, (
SELECT temp
FROM `bigquery-public-data.noaa_gsod.gsod2017` b
WHERE a.usaf=b.stn
ORDER BY da
LIMIT 1
) temp
FROM `bigquery-public-data.noaa_gsod.stations` a
LIMIT 10
修正:
#standardSQL
SELECT name, ARRAY_AGG(temp ORDER BY da LIMIT 1) temp
FROM `bigquery-public-data.noaa_gsod.stations` a
JOIN `bigquery-public-data.noaa_gsod.gsod2017` b
ON a.usaf=b.stn
GROUP BY 1
LIMIT 10
(给我一个公共数据集,我将编写一个与您的数据一起使用的查询)