|  | 
| 16 | 16 |     metadata.geo.subdivision2 AS subdivision2, | 
| 17 | 17 |     metadata.geo.country AS country | 
| 18 | 18 |     FROM | 
| 19 |  | -    {% raw %} | 
| 20 |  | -    {% if is_init() %} | 
| 21 |  | -    {% endraw %} | 
| 22 |  | -    `{{ project_id }}.{{ app_id }}_stable.baseline_v1` | 
| 23 |  | -    {% raw %} | 
| 24 |  | -    {% else %} | 
| 25 |  | -    {% endraw %} | 
| 26 | 19 |    `{{ project_id }}.{{ app_id }}_live.baseline_v1` | 
| 27 |  | -    {% raw %} | 
| 28 |  | -    {% endif %} | 
| 29 |  | -    {% endraw %} | 
| 30 | 20 |   WHERE | 
| 31 | 21 |     client_info.client_id IS NOT NULL | 
| 32 |  | -    {% raw %} | 
| 33 |  | -    {% if is_init() %} | 
| 34 |  | -    {% endraw %} | 
| 35 |  | -    AND sample_id = @sample_id | 
| 36 |  | -    AND DATE(submission_timestamp) <= CURRENT_DATE() | 
| 37 |  | -    {% raw %} | 
| 38 |  | -    {% else %} | 
| 39 |  | -    {% endraw %} | 
| 40 | 22 |     AND DATE(submission_timestamp) = @submission_date | 
| 41 | 23 |     AND 'automation' NOT IN ( | 
| 42 | 24 |       SELECT TRIM(t) FROM UNNEST(SPLIT(metadata.header.x_source_tags, ',')) t | 
| 43 | 25 |     ) | 
| 44 | 26 |   QUALIFY | 
| 45 | 27 |     ROW_NUMBER() OVER (PARTITION BY document_id ORDER BY submission_timestamp ) = 1 | 
| 46 |  | -    {% raw %} | 
| 47 |  | -    {% endif %} | 
| 48 |  | -    {% endraw %} | 
| 49 | 28 |    ), | 
| 50 | 29 |   overactive_{{ app_id }} AS ( | 
| 51 | 30 |   -- Find client_ids with over 150 000 pings in a day, | 
|  | 
| 56 | 35 |   FROM | 
| 57 | 36 |     base_{{ app_id }} | 
| 58 | 37 |   WHERE | 
| 59 |  | -    {% raw %} | 
| 60 |  | -    {% if is_init() %} | 
| 61 |  | -    {% endraw %} | 
| 62 |  | -    submission_date >= '2018-01-01' | 
| 63 |  | -    {% raw %} | 
| 64 |  | -    {% else %} | 
| 65 |  | -    {% endraw %} | 
| 66 | 38 |     submission_date = @submission_date | 
| 67 |  | -    {% raw %} | 
| 68 |  | -    {% endif %} | 
| 69 |  | -    {% endraw %} | 
| 70 | 39 |   GROUP BY | 
| 71 | 40 |     submission_date, | 
| 72 | 41 |     client_id | 
| @@ -100,56 +69,10 @@ WITH | 
| 100 | 69 |     -- `mode_last` can result in struct with all null values if it’s most frequent (or latest among ties). | 
| 101 | 70 |     -- This exclude structs with all null values so there will always be one non-NULL field. | 
| 102 | 71 |     AND COALESCE(city, subdivision1, subdivision2, country) IS NOT NULL | 
| 103 |  | -    {% raw %} | 
| 104 |  | -    {% if is_init() %} | 
| 105 |  | -    {% endraw %} | 
| 106 |  | -    AND submission_date >= '2018-01-01' | 
| 107 |  | -    {% raw %} | 
| 108 |  | -    {% else %} | 
| 109 |  | -    {% endraw %} | 
| 110 | 72 |     AND submission_date = @submission_date | 
| 111 |  | -    {% raw %} | 
| 112 |  | -    {% endif %} | 
| 113 |  | -    {% endraw %} | 
| 114 | 73 |    GROUP BY | 
| 115 | 74 |     submission_date, client_id, sample_id | 
| 116 | 75 |  ), | 
| 117 |  | -{% raw %} | 
| 118 |  | -{% if is_init() %} | 
| 119 |  | -{% endraw %} | 
| 120 |  | -  clients_city_first_seen_{{ app_id }} AS ( | 
| 121 |  | -  SELECT | 
| 122 |  | -    app_id, | 
| 123 |  | -    client_id, | 
| 124 |  | -    sample_id, | 
| 125 |  | -    submission_date AS first_seen_city_date, | 
| 126 |  | -    geo.city AS first_seen_city, | 
| 127 |  | -    geo.subdivision1 AS first_seen_subdivision1, | 
| 128 |  | -    geo.subdivision2 AS first_seen_subdivision2, | 
| 129 |  | -    geo.country AS first_seen_country | 
| 130 |  | -  FROM | 
| 131 |  | -    clients_daily_{{ app_id }} | 
| 132 |  | -  WHERE geo.city IS NOT NULL | 
| 133 |  | -  QUALIFY | 
| 134 |  | -    ROW_NUMBER() OVER (PARTITION BY client_id, sample_id ORDER BY submission_date ) = 1), | 
| 135 |  | -  clients_city_last_seen_{{ app_id }} AS ( | 
| 136 |  | -  SELECT | 
| 137 |  | -    app_id, | 
| 138 |  | -    client_id, | 
| 139 |  | -    sample_id, | 
| 140 |  | -    submission_date AS last_seen_city_date, | 
| 141 |  | -    geo.city AS last_seen_city, | 
| 142 |  | -    geo.subdivision1 AS last_seen_subdivision1, | 
| 143 |  | -    geo.subdivision2 AS last_seen_subdivision2, | 
| 144 |  | -    geo.country AS last_seen_country | 
| 145 |  | -  FROM | 
| 146 |  | -    clients_daily_{{ app_id }} | 
| 147 |  | -   WHERE geo.city IS NOT NULL | 
| 148 |  | -  QUALIFY | 
| 149 |  | -    ROW_NUMBER() OVER (PARTITION BY client_id, sample_id ORDER BY submission_date DESC) = 1 ){{ "," if not loop.last }} | 
| 150 |  | -  {% raw %} | 
| 151 |  | -  {% else %} | 
| 152 |  | -  {% endraw %} | 
| 153 | 76 |   _previous_{{ app_id }} AS ( | 
| 154 | 77 |     SELECT | 
| 155 | 78 |       * | 
|  | 
| 176 | 99 |     clients_daily_{{ app_id }} | 
| 177 | 100 |     WHERE | 
| 178 | 101 |       geo.city IS NOT NULL){{ "," if not loop.last }} | 
| 179 |  | - {% raw %} | 
| 180 |  | - {% endif %} | 
| 181 |  | - {% endraw %} | 
| 182 | 102 |   {% endfor -%} | 
| 183 |  | - {% raw %} | 
| 184 |  | - {% if is_init() %} | 
| 185 |  | - {% endraw %} | 
| 186 |  | - {% for app_id in app_id_list -%} | 
| 187 |  | -SELECT | 
| 188 |  | -  app_id, | 
| 189 |  | -  client_id, | 
| 190 |  | -  sample_id, | 
| 191 |  | -  first_seen_city_date, | 
| 192 |  | -  first_seen_city, | 
| 193 |  | -  first_seen_subdivision1, | 
| 194 |  | -  first_seen_subdivision2, | 
| 195 |  | -  first_seen_country, | 
| 196 |  | -  last_seen_city_date, | 
| 197 |  | -  last_seen_city, | 
| 198 |  | -  last_seen_subdivision1, | 
| 199 |  | -  last_seen_subdivision2, | 
| 200 |  | -  last_seen_country | 
| 201 |  | -FROM | 
| 202 |  | -  clients_city_first_seen_{{ app_id }} cfs | 
| 203 |  | -FULL OUTER JOIN | 
| 204 |  | -  clients_city_last_seen_{{ app_id }} cls | 
| 205 |  | -  USING (client_id, sample_id, app_id) | 
| 206 |  | -{{ "UNION ALL" if not loop.last }} | 
| 207 |  | -{% endfor -%} | 
| 208 |  | -{% raw %} | 
| 209 |  | -{% else %} | 
| 210 |  | -{% endraw %} | 
| 211 | 103 | {% for app_id in app_id_list -%} | 
| 212 | 104 | SELECT | 
| 213 | 105 | -- _p.* fields are NULL for clients that are not yet captured in the baseline_city_seen derived table. | 
| @@ -284,6 +176,4 @@ USING | 
| 284 | 176 |   (client_id, sample_id, app_id) | 
| 285 | 177 | {{ "UNION ALL" if not loop.last }} | 
| 286 | 178 | {% endfor -%} | 
| 287 |  | -{% raw %} | 
| 288 |  | -{% endif %} | 
| 289 |  | -{% endraw %} | 
|  | 179 | + | 
0 commit comments