【问题标题】:using EXISTS with alias in bigquery在 bigquery 中使用 EXISTS 和别名
【发布时间】:2019-03-04 02:26:00
【问题描述】:

我只是想通过使用 exists 子句来限制数据,但它似乎无法识别 EXISTS 子句中的别名(就像在 Oracle 中那样)

我正在使用这个 sql

select count(1) from 
 (select distinct 
         case when qp_cguid is null then ssid else qp_cguid end cguid,
         case when qp_vid is null then vid else qp_vid end as visit_id,
         TIMESTAMP_MILLIS(creation_date_time) visit_date_time
      from `zone1.table1` as t1
      where _partitiontime in ( timestamp( '2019-01-01') )
    and case when qp_vid is null then vid else qp_vid end is not null 
    and case when qp_cguid is null then ssid else qp_cguid end is not null 
    and exists
            (
             select 1 from `zone2.ga_temp` AS    ga 
             where ga.cguid = t1.cguid 
  and (t1.visit_date_time between TIMESTAMP_ADD(ga.min_hit_time_utc, INTERVAL -10 SECOND) and TIMESTAMP_ADD(ga.max_hit_time_utc, INTERVAL 10 SECOND))
            )
)      

这给了我这个错误

“在 t1 中找不到名称 cguid”

关于如何解决这个问题的任何想法?

【问题讨论】:

    标签: google-bigquery alias exists


    【解决方案1】:

    你可以试试这个方法

    WITH zone2 AS (
      select cguid from `zone2.ga_temp` AS ga 
    ),
    zone1 AS (
      select distinct 
       case when qp_cguid is null then ssid else qp_cguid end cguid,
       case when qp_vid is null then vid else qp_vid end as visit_id,
       TIMESTAMP_MILLIS(creation_date_time) visit_date_time
      from `zone1.table1` as t1
      where _partitiontime in ( timestamp( '2019-01-01') )
        and case when qp_vid is null then vid else qp_vid end is not null 
        and case when qp_cguid is null then ssid else qp_cguid end is not null 
    )
    SELECT count(1) FROM zone1 INNER JOIN zone2 ON zone1.cguid = zone2.cguid 
    WHERE 
      and (zone1.visit_date_time between TIMESTAMP_ADD(ga.min_hit_time_utc, INTERVAL -10 SECOND) and TIMESTAMP_ADD(ga.max_hit_time_utc, INTERVAL 10 SECOND))
    

    【讨论】:

    • @ Nik M. 希望我的回答对您有所帮助,注意:对 SO 很重要 - 您可以使用已发布答案左侧的勾号(投票下方)标记已接受的答案。请参阅meta 了解为什么它很重要
    猜你喜欢
    • 2019-05-15
    • 2022-07-06
    • 2010-09-06
    • 2019-01-12
    • 2012-10-22
    • 1970-01-01
    • 2011-10-28
    • 2021-08-29
    • 2013-05-08
    相关资源
    最近更新 更多