无法将此遗留 SQL 转换为 Google BigQuery 中的标准 SQL

Unable to convert this legacy SQL into Standard SQL in Google BigQuery

我无法将此遗留 sql 验证为标准 bigquery sql,因为我不知道这里还需要更改什么(如果我选择标准 [,此查询在验证期间失败 SQL 作为大查询方言):

SELECT
  lineitem.*,
  proposal_lineitem.*,
  porder.*,
  company.*,
  product.*,
  proposal.*,
  trafficker.name,
  salesperson.name,
  rate_card.*
FROM (
  SELECT
    *
  FROM
    dfp_data.dfp_order_lineitem
  WHERE
    DATE(end_datetime) >= DATE(DATE_ADD(CURRENT_TIMESTAMP(), -1, 'YEAR'))
    OR end_datetime IS NULL ) lineitem
JOIN (
  SELECT
    *
  FROM
    dfp_data.dfp_order) porder
ON
  lineitem.order_id = porder.id
LEFT JOIN (
  SELECT
    *
  FROM
    adpoint_data.dfp_proposal_lineitem) proposal_lineitem
ON
  lineitem.id = proposal_lineitem.dfp_lineitem_id
JOIN (
  SELECT
    *
  FROM
    dfp_data.dfp_company) company
ON
  porder.advertiser_id = company.id
LEFT JOIN (
  SELECT
    *
  FROM
    adpoint_data.dfp_product) product
ON
  proposal_lineitem.product_id=product.id
LEFT JOIN (
  SELECT
    *
  FROM
    adpoint_data.dfp_proposal) proposal
ON
  proposal_lineitem.proposal_id=proposal.id
LEFT JOIN (
  SELECT
    *
  FROM
    adpoint_data.dfp_rate_card) rate_card
ON
  proposal_lineitem.ratecard_id=rate_card.id
LEFT JOIN (
  SELECT
    id,
    name
  FROM
    dfp_data.dfp_user) trafficker
ON
  porder.trafficker_id =trafficker.id
LEFT JOIN (
  SELECT
    id,
    name
  FROM
    dfp_data.dfp_user) salesperson
ON
  porder. salesperson_id =salesperson.id

很可能您遇到的错误如下所示

Duplicate column names in the result are not supported. Found duplicate(s): name   

Legacy SQL 将 SELECT 语句中的 trafficker.namesalesperson.name 分别调整为 trafficker_namesalesperson_name 从而有效地消除列名重复

标准 SQL 的行为不同,将这两个列都命名为 name,从而产生重复的情况。为避免这种情况 - 您只需提供别名,如下例所示

SELECT
  lineitem.*,
  proposal_lineitem.*,
  porder.*,
  company.*,
  product.*,
  proposal.*,
  trafficker.name AS trafficker_name,
  salesperson.name AS salesperson_name,
  rate_card.*
FROM ( ...

您可以使用以下 simplified/dummy 查询轻松检查上面的解释

#legacySQL
SELECT
  porder.*,
  trafficker.name,
  salesperson.name
FROM (
  SELECT 1 order_id, 'abc' order_name, 1 trafficker_id, 2 salesperson_id
) porder
LEFT JOIN (SELECT 1 id, 'trafficker' name) trafficker
  ON porder.trafficker_id =trafficker.id
LEFT JOIN (SELECT 2 id, 'salesperson' name ) salesperson
  ON porder. salesperson_id =salesperson.id

#standardSQL
SELECT
  porder.*,
  trafficker.name AS trafficker_name,
  salesperson.name AS salesperson_name
FROM (
  SELECT 1 order_id, 'abc' order_name, 1 trafficker_id, 2 salesperson_id
) porder
LEFT JOIN (SELECT 1 id, 'trafficker' name) trafficker
  ON porder.trafficker_id =trafficker.id
LEFT JOIN (SELECT 2 id, 'salesperson' name ) salesperson
  ON porder. salesperson_id =salesperson.id  

注意:如果您有更多重复名称 - 您也需要为所有这些名称起别名