BigQuery要求JOIN EACH / GROUP EACH,而它已经在任何地方使用

时间:2016-07-08 11:41:55

标签: google-bigquery

我正在运行可能非常繁重的查询(包括CROSS JOIN)。它处理了少量数据(来自今天的事件),但是当我在较长时间的数据上尝试时,它会在几分钟之后失败,要求我使用JOIN EACH。

但正如您在下面所看到的,一切都已经是JOIN EACH或GROUP EACH BY。对于Google员工,这里有一份最近的职位ID: vex-production:bquijob_16f3fe2c_155ca481056

SELECT
  part1.user_id AS user_id,
  brand_id,
  (2 * SV / PS + 1 * PV / TP + 5 * C / SV + 10 * O / AO) / 18 * 100 AS score
FROM (
  SELECT
    user_id,
    brand_id,
    COUNT(DISTINCT sale_id) AS PS,
    COUNT(DISTINCT IF(user_clicked_sale IS NULL, NULL, sale_id)) AS SV,
    COUNT(DISTINCT IF(user_clicked_product IS NULL, NULL, product_id)) AS PV,
    COUNT(DISTINCT IF(user_clicked_sale IS NULL, NULL, product_id)) AS TP,
    COUNT(DISTINCT order_id) AS C,
    COUNT(DISTINCT IF(order_state = 'Ordered', order_id, NULL)) AS O
  FROM (
    SELECT
      brands.product_brand_id AS brand_id,
      brand_products.sale_id AS sale_id,
      brand_products.product_id AS product_id,
      potential_sales.user_id AS user_id,
      viewed_sales.user_id AS user_clicked_sale,
      viewed_products.user_id AS user_clicked_product,
      carts.order_id AS order_id,
      carts.order_state AS order_state,
      carts.items_ordered AS items_ordered
    FROM (
      SELECT
        product_brand_id
      FROM
        [vex-production:products.all]
      GROUP EACH BY
        product_brand_id) AS brands
    LEFT JOIN EACH (
      SELECT
        product_brand_id,
        sale_id,
        product_id
      FROM
        [vex-production:products.all]
      GROUP EACH BY
        product_brand_id,
        sale_id,
        product_id) AS brand_products
    ON
      brand_products.product_brand_id = brands.product_brand_id
    LEFT JOIN EACH (
      SELECT
        user_id,
        sale_id
      FROM (
        SELECT
          sale_id,
          IFNULL(sale_early_access, sale_start_time) AS sale_open_time,
          sale_end_time AS sale_close_time
        FROM
          [vex-production:products.all]
        GROUP EACH BY
          sale_id,
          sale_open_time,
          sale_close_time ) AS sales
      CROSS JOIN (
        SELECT
          user_id,
          session,
          MIN(event_datetime) AS session_start,
          MAX(event_datetime) AS session_end
        FROM
          [vex-production:views.last_4_months]
        WHERE
          user_id IS NOT NULL
        GROUP EACH BY
          user_id,
          session ) AS sessions
      WHERE
        (session_start >= sale_open_time
          AND session_end < sale_close_time)
        OR (session_start < sale_open_time
          AND session_end > sale_open_time
          AND session_end < sale_close_time)
        OR (session_start > sale_open_time
          AND session_start < sale_close_time
          AND session_end > sale_close_time) ) AS potential_sales
    ON
      potential_sales.sale_id = brand_products.sale_id
    LEFT JOIN EACH (
      SELECT
        user_id,
        sale_id
      FROM
        [vex-production:views.last_4_months]
      WHERE
        user_id IS NOT NULL
      GROUP EACH BY
        user_id,
        sale_id ) AS viewed_sales
    ON
      viewed_sales.sale_id = brand_products.sale_id
      AND potential_sales.user_id = viewed_sales.user_id
    LEFT JOIN EACH (
      SELECT
        user_id,
        product_id
      FROM
        [vex-production:views.last_4_months]
      WHERE
        user_id IS NOT NULL
      GROUP EACH BY
        user_id,
        product_id ) AS viewed_products
    ON
      viewed_products.product_id = brand_products.product_id
      AND potential_sales.user_id = viewed_products.user_id
    LEFT JOIN EACH (
      SELECT
        user_id,
        sale_id,
        order_id,
        order_state,
        SUM(quantity) AS items_ordered
      FROM
        [vex-production:carts.all]
      GROUP EACH BY
        user_id,
        sale_id,
        order_id,
        order_state ) AS carts
    ON
      carts.user_id = potential_sales.user_id
      AND carts.sale_id = brand_products.sale_id )
  GROUP EACH BY
    user_id,
    brand_id ) part1
LEFT JOIN EACH (
  SELECT
    user_id,
    COUNT(DISTINCT order_id) AS AO
  FROM
    [vex-production:carts.complete]
  GROUP EACH BY
    user_id ) part2
ON
  part1.user_id = part2.user_id

1 个答案:

答案 0 :(得分:2)

问题的根源很可能是CROSS JOIN(不允许其中包含EACH - CROSS JOIN EACH is not supported
我建议&#34;重写&#34;您使用BigQuery Standard SQL进行查询,因此CROSS JOINWHERE的{​​{1}}只会JOIN ON。对于您的特定查询,这应该非常简单

检查Enabling Standard SQL 并且不要忘记更改表格引用
来自[vex-production:carts.complete] 到'vex-production.carts.complete`