Skip to content
Advertisement

Filter country using a JOIN in BigQuery

In a scheduled query in BQ, I am trying to filter countries using an external Google Sheet (already made a table). The query already has a join from two other tables. I would like to enter a SELECT clause in the WHERE statement.

SELECT
    DATE(e_time) AS date,
    DATE(i_time) AS i_date,
    DATE_DIFF(DATE(e_time),DATE(i_time),day) AS days_since_install,
    country_code AS country,
  FROM
    `data.sessions`
  WHERE
    DATE(e_time)= DATE_SUB(CURRENT_DATE(), INTERVAL 2 day)
    AND country_code = (
    SELECT
      country_code as country
    FROM
      `data.sessions` a
   INNER JOIN
      `data.country_filter` b
    ON
      string_field_0 = a.country_code)

I keep getting errors. Any tips as to where I am going wrong? Expected output would be table 1 with the filtered countries from table b. Thank you in advance.

Advertisement

Answer

in your query you have to use in

  SELECT
        DATE(e_time) AS date,
        DATE(i_time) AS i_date,
        DATE_DIFF(DATE(e_time),DATE(i_time),day) AS days_since_install,
        country_code AS country,
      FROM
        `data.sessions`
      WHERE
        DATE(e_time)= DATE_SUB(CURRENT_DATE(), INTERVAL 2 day)
        AND country_code in(
        SELECT
          country_code as country
        FROM
          `data.sessions` a
       INNER JOIN
          `data.country_filter` b
        ON
          string_field_0 = a.country_code)
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement