beetlejuice
beetlejuice

Reputation: 161

Google BigQuery SQL: "Field not found" on field with Concat function

I have an issue with BigQuery not able to identify a field key I specify which uses a Concat string:

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

I have also noticed that using a simple "Where" function (Key1 IS NOT NULL) again returns an error where the field is not found.

Any ideas on how I can get around this issue?

Upvotes: 0

Views: 112

Answers (1)

beetlejuice
beetlejuice

Reputation: 161

Got it to work. It seems I missed the fact I needed to declare the concatenated field first in the first table:

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

In any event, the CASE statement was not necessary (I added it as I thought that would fix the problem). Here is the revised and more accurate query:

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

Upvotes: 1

Related Questions