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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
Duplicate column names in the result are not supported. Found duplicate(s): name

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

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

1
2
3
4
5
6
7
8
9
10
11
SELECT
  lineitem.*,
  proposal_lineitem.*,
  porder.*,
  company.*,
  product.*,
  proposal.*,
  trafficker.name AS trafficker_name,
  salesperson.name AS salesperson_name,
  rate_card.*
FROM ( ...

您可以使用以下简化/虚拟查询轻松检查上述说明

1
2
3
4
5
6
7
8
9
10
11
12
#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

1
2
3
4
5
6
7
8
9
10
11
12
#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

注意:如果你有更多重复的名字 - 你也需要给它们起别名