Avoid correlated subqueries error in BigQuery

前端 未结 2 1240
既然无缘
既然无缘 2020-12-17 17:50

I have a simple query to obtain the currency rate in use at the time a transaction was created:

SELECT t.orderid, t.date, 
 (SELECT rate FROM sources.currenc         


        
相关标签:
2条回答
  • 2020-12-17 18:05

    Below is for BigQuery Standard 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
    
    0 讨论(0)
  • 2020-12-17 18:23

    I've noticed similar behavior with other correlated subqueries. They are useful, but can't always be automatically modeled to JOINs by BigQuery.

    Similar case which works:

    #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
    

    Doesn't work:

    #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
    

    Fix:

    #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
    

    (give me a public dataset, and I'll write a query that works with your data)

    0 讨论(0)
提交回复
热议问题