大查询标准SQL中的子选择

时间:2019-11-04 13:58:16

标签: sql google-bigquery

我似乎无法解决此SQL问题。我有子选择查询,想添加一个where子句:

SELECT 

  -- Custom Dimension Canonical URL (Hit)
 (
    SELECT
      value
    FROM
      UNNEST(hits.customDimensions)
    WHERE
      index = 1
    GROUP BY
      1
  ) AS canonicalURL,

  -- Custom Dimension Publishing Date (Hit)
  (
    SELECT
      value
    FROM
      UNNEST(hits.customDimensions)
    WHERE
      index = 8
    GROUP BY
      1
  ) AS articlePublishingDate

FROM
  ${constants.ga_tables} AS session,
  UNNEST(hits) AS hits
WHERE
  _table_suffix BETWEEN '20191103'
  AND FORMAT_DATE(
    '%Y%m%d',
    DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY)
  )
  AND totals.visits = 1

GROUP BY
  1,
  2

在外面的where子句中,我想过滤     articlePublishingDate不为null 像这样:

FROM
  ${constants.ga_tables} AS session,
  UNNEST(hits) AS hits
WHERE
  _table_suffix BETWEEN '20191103'
  AND FORMAT_DATE(
    '%Y%m%d',
    DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY)
  )
  AND totals.visits = 1 AND articlePublishingDate is not null

但是,我收到“无法识别的名称”错误。您有解决办法吗?

1 个答案:

答案 0 :(得分:1)

您可以尝试使用以下任意一种方法来代替AND articlePublishingDate is not null

AND EXISTS (SELECT 1 FROM UNNEST(arr) WHERE index = 8)   

AND 8 IN (SELECT index FROM UNNEST(arr))