BigQuery,联盟问题/错误:语法错误:预期关键字ALL或关键字DISTINCT,但关键字SELECT的位置为[16:3]

时间:2018-06-22 13:56:04

标签: sql google-bigquery union

当前,我正在尝试将4个表连接在一起,仅显示5列,并将它们全部合并到一个表中。

这是查询:

SELECT
  id,
  platform,
  url,
  profileImageUrl,
  name
FROM (
  SELECT
    f.id AS id,
    f.platform AS platform,
    f.url AS url,
    f.profileImageUrl AS profileImageUrl,
    f.name AS name
  FROM
    `l.p.link_f_main` AS f UNION
  SELECT
    i.id AS id,
    i.platform AS platform,
    i.url AS url,
    i.profilePicture AS profileImageUrl,
    i.fullName AS name
  FROM
    `l.p.link_i_main` AS i UNION
  SELECT
    t.id AS id,
    t.platform AS platform,
    t.url AS url,
    t.profileImageUrl AS profileImageUrl,
    t.name AS name
  FROM
    `l.p.link_t_main` AS t UNION
  SELECT
    y.id AS id,
    y.platform AS platform,
    y.url AS url,
    y.profileImageUrl AS profileImageUrl,
    y.name AS name
  FROM
    `l.p.link_y_main` AS y ) as main

这是错误:

Error: Syntax error: Expected keyword ALL or keyword DISTINCT but got keyword SELECT at [16:3]

我做错了什么?

2 个答案:

答案 0 :(得分:1)

答案:

1)对于大查询,必须使用UNION ALL而不是UNION

2)所有联合列的数据类型应该相同。     -

  1. 我遇到一个问题,列中的一个是STRING-INT64, INT64, INT64, STRING
  2. 因此,我也将其余列更改为STRING,以使其正常工作。

答案 1 :(得分:0)

摘自文档:UNION { ALL | DISTINCT }。因此,您必须使用UNION ALLUNION DISTINCT(这正是错误消息所说的)。正如HoneyBadger用户提到的那样,这是正确的解释。