簡體   English   中英

BigQuery - 使用子查詢和OR語句加入多個條件

[英]BigQuery - Joining on multiple conditions using subqueries and OR statements

無論如何在多個潛在條件下加入兩個表格?

我目前正在將一些代碼從Postgres遷移到Bigquery,我加入了多個潛在的值,如:

SELECT
 *
FROM
 (
 SELECT
   offer_table.offer_id
   ,customer_table.customer_name
   ,customer_table.visit_count
   ,ROW_NUMBER() OVER (PARTITION BY offer_table.offer_id ORDER BY customer_table.visit_count DESC) AS customer_visit_rank
 FROM
   offer_table
   LEFT JOIN customer_table ON
    (
    offer_table.customer_id = customer_table.customer_id
    OR offer_table.email = customer_table.email
    OR offer_table.phone = customer_table.phone
    )
 ) dummy
WHERE
  customer_visit_rank = 1

我需要這個,因為我的報價和客戶數據對我們的ID,電子郵件和電話字段的使用不一致,但都是有效的潛在匹配。 如果多個字段有效(例如:id和電子郵件匹配),則會有重復的行,我會在使用ORDER BY部分排序后根據row_number列過濾掉它們。

但是當我嘗試在BigQuery中加入多個條件時,我收到以下錯誤消息:

LEFT OUTER JOIN cannot be used without a condition that is an equality of fields from both sides of the join.

有沒有人想出一個加入多個值的解決方案而不是上面做的?

您可以編寫單獨的查詢,然后使用COALESCE

SELECT
  *
FROM
  (
    SELECT
      offer_table.offer_id
      ,COALESCE(c1.customer_name,c2.customer_name,c3.customer_name)
      ,COALESCE(c1.visit_count,c2.visit_count,c3.visit_count)
      ,ROW_NUMBER() OVER (PARTITION BY offer_table.offer_id ORDER BY customer_table.visit_count DESC) AS customer_visit_rank
    FROM
      offer_table
    LEFT JOIN customer_table c1
      ON offer_table.customer_id = customer_table.customer_id
    LEFT JOIN customer_table c2
      ON offer_table.email = customer_table.email
    LEFT JOIN customer_table c3
      ON offer_table.phone = customer_table.phone
   )
 ) AS dummy
WHERE
  customer_visit_rank = 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM