【问题标题】:How to use column alias in where clause如何在where子句中使用列别名
【发布时间】:2020-05-14 20:46:37
【问题描述】:

我正在尝试删除所有包含 productRef = productAssociated 的行。
我尝试了下面的查询,但最后一行不起作用。

怎么了?

SELECT date, transaction.transactionId, 
       ref.productSKU as productRef, 
       associated.productSKU as productAssociated, 
       ARRAY_LENGTH(hits.product) as nbProducts
FROM `dl-recommendation-engine.NDA_CHANEL_137002018.ga_sessions_*` as session,
     UNNEST(hits) AS hits,
     UNNEST(hits.product) as ref,
     UNNEST(hits.product) as associated
WHERE _TABLE_SUFFIX BETWEEN '20191122' AND '20191202' AND
      hits.transaction.transactionId IS NOT NULL AND
     ARRAY_LENGTH(hits.product) > 2 AND
     productAssociated != productRef

【问题讨论】:

  • 有关参考,请参阅Problems with Column Aliases:“标准 SQL 不允许在 WHERE 子句中引用列别名。施加此限制是因为在评估 WHERE 子句时,可能尚未确定列值。”

标签: sql google-bigquery cross-join


【解决方案1】:

您不能在where 子句中使用表别名。

相反,只需使用以下表达式:

WHERE _TABLE_SUFFIX BETWEEN '20191122' AND '20191202' AND
      hits.transaction.transactionId IS NOT NULL AND
      ARRAY_LENGTH(hits.product) > 2 AND
      associated.productSKU <> ref.productSKU

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 2012-01-12
    • 2011-03-15
    • 2010-09-26
    • 2012-10-13
    相关资源
    最近更新 更多