简体   繁体   English

BigQuery - 如何避免相关子查询错误?

[英]BigQuery - How to Avoid correlated subqueries error?

I'm trying to run a query that works on my database, but not on Bigquery, I get this error.我正在尝试运行适用于我的数据库但不适用于 Bigquery 的查询,我收到此错误。

Correlated subqueries that reference other tables are not supported unless they can be de-correlated, such as by transforming them into an efficient JOIN.不支持引用其他表的相关子查询,除非它们可以去相关,例如通过将它们转换为有效的 JOIN。

I have a table with historical prices of Bitcoin for each minute (timestamp and price) and another table with timestamp, block number and a empty column with price and I try to assign a price to each block, since the seconds appear in the blocks, I try to assign the price closest to that minute.我有一张每分钟的比特币历史价格表(时间戳和价格)和另一个带有时间戳、块号和一个空列的价格表,我尝试为每个块分配一个价格,因为秒出现在块中,我尝试指定最接近那一分钟的价格。

here goes my code这是我的代码

UPDATE bitcoin_data.bloques SET price_usd = 
    (
        SELECT close AS price_usd_final 
        FROM 
            (
                (SELECT timestamp, close FROM bitcoin_data.Precio AS precio WHERE timestamp >= bloques.timestamp ORDER BY timestamp LIMIT 1) 
                UNION ALL
                (SELECT timestamp, close FROM bitcoin_data.Precio AS precio WHERE timestamp <  bloques.timestamp  ORDER BY timestamp DESC LIMIT 1)
            ) AS prices 
        ORDER BY ABS(EXTRACT(DAY FROM bloques.timestamp - timestamp))
    ) 
WHERE price_usd IS NULL

You may use below approach to match the price from another table to the blocks based on the closest timestamp between your 2 tables.您可以使用以下方法根据您的 2 个表之间最接近的时间戳将另一个表中的priceblocks匹配。

WITH `project.dataset.bloques` AS (
  SELECT TIMESTAMP '2019-07-01 08:45:09' bloques_timestamp, 'A' bloques UNION ALL
  SELECT '2019-07-01 09:15:15', 'B' UNION ALL
  SELECT '2019-07-01 10:30:29', 'C' 
), `project.dataset.precio` AS (
  SELECT TIMESTAMP '2019-07-01 08:45:35' precio_timestamp, '1000' precio UNION ALL
  SELECT '2019-07-01 09:15:45', '4000' UNION ALL
  SELECT '2019-07-01 10:30:01', '5000' 
)
SELECT * FROM (
  SELECT IF(
    ts - LAST_VALUE(ts IGNORE NULLS) OVER(prev_win) < FIRST_VALUE(ts IGNORE NULLS) OVER(next_win) - ts, 
    LAST_VALUE(bloques_timestamp IGNORE NULLS) OVER(prev_win), FIRST_VALUE(bloques_timestamp IGNORE NULLS) OVER(next_win)
    ) bloques_timestamp, IF(
    ts - LAST_VALUE(ts IGNORE NULLS) OVER(prev_win) < FIRST_VALUE(ts IGNORE NULLS) OVER(next_win) - ts, 
    LAST_VALUE(bloques IGNORE NULLS) OVER(prev_win), FIRST_VALUE(bloques IGNORE NULLS) OVER(next_win)
    ) bloques, precio 
  FROM (
    SELECT bloques_timestamp, UNIX_SECONDS(bloques_timestamp) ts, bloques, '' precio FROM `project.dataset.bloques` 
    UNION ALL
    SELECT precio_timestamp, UNIX_SECONDS(precio_timestamp), NULL, precio FROM `project.dataset.precio` 
  )
  WINDOW 
    prev_win AS (ORDER BY bloques_timestamp ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING),
    next_win AS (ORDER BY bloques_timestamp ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING)
)
WHERE precio != ''  

However, you are encountering the error because according to this documentation ,但是,您遇到错误是因为根据此文档

FROM clause aliases are not visible to subqueries in the same FROM clause. FROM 子句别名对同一 FROM 子句中的子查询不可见。 Subqueries in a FROM clause cannot contain correlated references to other tables in the same FROM clause. FROM 子句中的子查询不能包含对同一 FROM 子句中其他表的相关引用。

As a suggestion, you may use WITH inside your subquery to avoid correlated subqueries error.作为建议,您可以在子查询中使用WITH以避免相关子查询错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 避免 BigQuery 中的相关子查询错误 - Avoid correlated subqueries error in BigQuery BigQuery 参数化子查询 - Bigquery parameterized subqueries 如何通过流式插入避免 BigQuery 中的重复 - how to avoid duplication in BigQuery by streaming insert 如何避免 [缺少关闭双引号 (") 字符。] google bigquery 中的错误 - how to avoid [Missing close double quote (") character.] error in google bigquery 在 BigQuery SQL 中,如何跟踪资源使用情况以避免“查询执行期间超出资源”错误 - In BigQuery SQL, how to track resource usage to avoid "Resources exceeded during query execution" error 在 BigQuery 中对大的 integer 值应用 sum() 时如何避免 integer 溢出错误 - How to avoid integer overflow error when applying sum() over large integer values in BigQuery 如何在 bigquery 中动态连接表以避免公共列重复 - how to dynamically join tables in bigquery to avoid duplication of common columns 如何避免 Grafana 将来自 Google BigQuery 的 NULL 值处理为 0 - How to avoid Grafana handling NULL values from Google BigQuery as 0 避免 session 关闭 BigQuery 存储 API 与 Dataflow - Avoid session shutdown on BigQuery Storage API with Dataflow 将算法打包到 Bigquery SQL UDF 时,如何避免性能急剧下降? - How can I avoid drastic decrease in performance when packaging algorithms into a Bigquery SQL UDFs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM