Google BigQuery SQL:具有Concat功能的字段上的“找不到字段”

时间:2019-02-25 20:00:42

标签: google-bigquery

我遇到BigQuery无法识别我指定的使用Concat字符串的字段键的问题:

SELECT
T2.MinHit,
CONCAT(CAST(T1.visitId AS STRING), CASE WHEN T1.hits.page.searchKeyword IS NULL THEN "(NULL)" ELSE T1.hits.page.searchKeyword END) AS Key1,
CONCAT("https://", T1.hits.page.hostname, T1.hits.page.pagePath) AS urlPage
FROM [bigquery-public-data:google_analytics_sample.ga_sessions_20170801] AS T1
INNER JOIN (
   SELECT
   CONCAT(CAST(visitId AS STRING), CASE WHEN hits.page.searchKeyword IS NULL THEN "(NULL)" ELSE hits.page.searchKeyword END) AS Key2,
   MIN(hits.hitNumber) AS MinHit
   FROM [bigquery-public-data:google_analytics_sample.ga_sessions_20170801]
   GROUP BY Key1 ) AS T2
ON T1.Key1 = T2.Key2

我还注意到,使用简单的“ Where”功能(Key1 IS NOT NULL)再次返回找不到字段的错误。

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

1 个答案:

答案 0 :(得分:1)

开始工作。似乎我错过了需要在第一个表中首先声明串联字段的事实:

SELECT
T1.SKey,
T1.urlPage,
T2.MinHit
FROM 
(
  SELECT 
  CONCAT(CAST(visitId AS STRING),CASE WHEN hits.page.searchKeyword IS NULL THEN "(NULL)" ELSE hits.page.searchKeyword END) as SKey, 
  CONCAT("https://", hits.page.hostname, hits.page.pagePath) as urlPage,
  FROM [bigquery-public-data:google_analytics_sample.ga_sessions_20170801] 
  GROUP BY SKey, urlPage
)
AS T1 
INNER JOIN 
        ( 
        SELECT 
        CONCAT(CAST(visitId AS STRING),CASE WHEN hits.page.searchKeyword IS NULL THEN "(NULL)" ELSE hits.page.searchKeyword END) as SKey, 
        MIN(hits.hitNumber) as MinHit 
        FROM [bigquery-public-data:google_analytics_sample.ga_sessions_20170801] 
        GROUP BY SKey 
        ) AS T2 
ON T1.SKey = T2.SKey

无论如何,都不需要CASE语句(我添加了它是因为我认为这样可以解决问题)。 这是经过修改且更准确的查询:

SELECT
T1.SKey,
T1.urlPage,
T2.MinHit
FROM 
(
  SELECT 
  CONCAT(CAST(visitId AS STRING), hits.page.searchKeyword) as SKey, 
  CONCAT("https://", hits.page.hostname, hits.page.pagePath) as urlPage,
  FROM [bigquery-public-data:google_analytics_sample.ga_sessions_20170801] 
  GROUP BY SKey, urlPage
)
AS T1 
INNER JOIN 
        ( 
        SELECT 
        CONCAT(CAST(visitId AS STRING), hits.page.searchKeyword) as SKey, 
        MIN(hits.hitNumber) as MinHit 
        FROM [bigquery-public-data:google_analytics_sample.ga_sessions_20170801] 
        GROUP BY SKey 
        ) AS T2 
ON T1.SKey = T2.SKey