在bigquery中将EXISTS与别名一起使用

时间:2019-03-04 02:26:00

标签: google-bigquery alias exists

我只是试图通过使用exist子句来限制数据,但是它似乎无法识别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”

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

1 个答案:

答案 0 :(得分:0)

您可以尝试这种方法

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))