无法将旧版SQL转换为Google BigQuery中的标准SQL

时间:2019-03-25 06:08:23

标签: google-bigquery standard-sql

我无法将此旧版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

1 个答案:

答案 0 :(得分:1)

您遇到的错误很可能是以下

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

旧版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 ( ...

您可以使用下面的简化/虚拟查询轻松查看上面解释的内容

#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  

注意:如果您有更多重复的名称-您也需要为所有名称都加上别名