FULL OUTER JOIN抛出错误"在括号中包裹选择"

时间:2017-07-06 10:43:11

标签: sql google-bigquery

我在部分SQL调用时遇到问题,收到此错误  Error: Syntax error: Each subquery argument for table-valued function calls must be enclosed in parentheses. To fix this, replace SELECT... with (SELECT...) at [32:5]
这是SELECT之后的FULL OUTER JOIN EACH,我认为我已经这样做了,我不知道这里有什么问题,所以任何建议都会受到很大关注。

我正在尝试创建一个漏斗,以便更精确地从以前的客户中分类。漏斗共有3个级别,简单性为"我只会展示两个。

SELECT
 COUNT(s0.firstHit) AS pageId1,
 SUM(s0.exit) AS pageId2,
 COUNT(s1.firstHit) AS pageId3,
 SUM(s1.exit) AS pageId4
FROM(
  SELECT
    s0.fullVisitorId,
    s0.visitId,
    s0.firstHit,
    s0.exit,
    s1.firstHit,
    s1.exit
  FROM (
    SELECT
      fullvisitorid,
      visitid,
      (SELECT MIN(hitNumber) FROM UNNEST(hits) WHERE REGEXP_CONTAINS(page.pagePath, r'pageId')) AS firstHit,
      (SELECT MAX(IF(isExit, 1, 0)) FROM UNNEST(hits) WHERE REGEXP_CONTAINS(page.pagePath, r'pageId')) AS exitFlag
    FROM
      `<ID>.ga_sessions_2017*`
    WHERE
      _TABLE_SUFFIX BETWEEN '0601' AND '0602'
      AND 1 = 1
      AND EXISTS(SELECT 1 FROM UNNEST(hits) WHERE REGEXP_CONTAINS(page.pagePath, r'pageId'))
  AND EXISTS(SELECT 1 FROM UNNEST(hits) hits WHERE (SELECT COUNT(value) FROM UNNEST(hits.customDimensions) custd WHERE index=20) > 0)
  AND totals.visits = 1
  GROUP BY
    fullVisitorId,
    visitId) AS s0
  FULL OUTER JOIN EACH(
  SELECT
    fullVisitorId,
    visitId,
    (SELECT MIN(hitNumber) FROM UNNEST(hits) WHERE REGEXP_CONTAINS(page.pagePath, 'pageId')) AS firstHit,
    (SELECT MAX(IF(isExit, 1, 0)) FROM UNNEST(hits) WHERE REGEXP_CONTAINS(page.pagePath, 'pageId')) AS exitFlag
  FROM
    `<ID>.ga_sessions_2017*`
  WHERE
    _TABLE_SUFFIX BETWEEN '0601' AND '0602'
    AND totals.visits = 1
  GROUP BY
    fullVisitorId,
    visitId) AS s1
ON
  s0.fullVisitorId = s1.fullVisitorId
  AND s0.visitId = s1.visitId ) s01    

3 个答案:

答案 0 :(得分:1)

您可以找到编写此查询的方法,而不是进行JOIN操作。

例如:

SELECT
  fullvisitorid,
  visitid,
  (SELECT MIN(hitNumber) FROM UNNEST(hits) WHERE REGEXP_CONTAINS(page.pagePath, r'pageId')) AS firstFunnelHit,
  (SELECT MAX(IF(isExit, 1, 0)) FROM UNNEST(hits) WHERE REGEXP_CONTAINS(page.pagePath, r'pageId')) AS firstExitFunnelFlag,
  (SELECT MIN(hitNumber) FROM UNNEST(hits) WHERE (REGEXP_CONTAINS(page.pagePath, r'pageId')) OR ((select count(1) from unnest(hits) h, unnest(h.customDimensions) custd where custd.index = 20) > 0)) secondFunnelHit,
  (SELECT MAX(IF(isExit, 1, 0)) FROM UNNEST(hits) WHERE REGEXP_CONTAINS(page.pagePath, r'pageId') OR ((select count(1) from unnest(hits) h, unnest(h.customDimensions) custd where custd.index = 20) > 0)) AS secondFunnelExitFlag
FROM `dataset.ga_sessions_2017*`
WHERE 1 = 1
  AND _TABLE_SUFFIX BETWEEN '0601' AND '0602'
  AND totals.visits = 1

请注意,只需一个SELECT,您就可以提供有关所有访问过“pageId”页面的访问者的信息,以及访问此页面并在customDimensionindex=20的访问者}。

对于渠道分析中的每个步骤,您都可以将新列作为结果,例如firstFunnelHitsecondFunnelHit

通过避免昂贵的JOIN,您可以查询数据,但仍可在几秒钟内获得结果。

答案 1 :(得分:0)

在最后一个子查询中:

  SELECT
    fullVisitorId,
    visitId
    (SELECT MIN(hitNumber) FROM UNNEST(hits) WHERE REGEXP_CONTAINS(page.pagePath, 'pageId')) AS firstHit,
    (SELECT MAX(IF(isExit, 1, 0)) FROM UNNEST(hits) WHERE REGEXP_CONTAINS(page.pagePath, 'pageId')) AS exitFlag
  FROM
    `<ID>.ga_sessions_2017*`
  WHERE
    _TABLE_SUFFIX BETWEEN '0601' AND '0602'
    AND totals.visits = 1
  GROUP BY
    fullVisitorId,
    visitId

在我看来,你需要在第三行visitId之后使用逗号。

祝你好运。

答案 2 :(得分:0)

使用标准SQL时没有EACH个关键字;这是遗留SQL特有的。删除该单词,您的查询可能会有效。