WITH search_clients_daily AS (
  SELECT
    submission_date,
    client_id,
    ANY_VALUE(country) AS country,
    ANY_VALUE(mozfun.norm.os(os)) AS os,
    ANY_VALUE(channel) AS channel,
    ANY_VALUE(locale) AS locale,
    CAST(MAX(mozfun.norm.extract_version(app_version, "major")) AS INT64) AS app_version,
    ANY_VALUE(
      `moz-fx-data-shared-prod`.udf.normalize_search_engine(default_search_engine)
    ) AS default_search_engine,
    ANY_VALUE(DATE_FROM_UNIX_DATE(profile_creation_date)) AS profile_creation_date,
    LOGICAL_OR(is_default_browser) AS is_default_browser,
    LOGICAL_OR(is_sap_monetizable) AS is_sap_monetizable,
    LOGICAL_OR(has_adblocker_addon) AS has_adblocker_addon,
    LOGICAL_OR(policies_is_enterprise) AS policies_is_enterprise,
    SUM(subsession_hours_sum) AS subsession_hours_sum,
    SUM(sessions_started_on_this_day) AS sessions_started_on_this_day,
    SUM(active_addons_count_mean) AS active_addons_count_mean,
    SUM(max_concurrent_tab_count_max) AS max_concurrent_tab_count_max,
    SUM(active_hours_sum) AS active_hours_sum,
    SUM(total_uri_count) AS total_uri_count,
    SUM(organic) AS organic,
    SUM(tagged_sap) AS tagged_sap,
    SUM(tagged_follow_on) AS tagged_follow_on,
    SUM(sap) AS sap,
    SUM(ad_click) AS ad_click,
    SUM(ad_click_organic) AS ad_click_organic,
    SUM(search_with_ads) AS search_with_ads,
    SUM(search_with_ads_organic) AS search_with_ads_organic,
  FROM
    `moz-fx-data-shared-prod.search_derived.search_clients_daily_v8`
  WHERE
    submission_date = @submission_date
  GROUP BY
    submission_date,
    client_id
),
newtab_clients_daily AS (
  SELECT
    submission_date,
    legacy_telemetry_client_id AS client_id,
    ANY_VALUE(country_code) AS country_code,
    ANY_VALUE(normalized_os) AS normalized_os,
    ANY_VALUE(channel) AS channel,
    ANY_VALUE(locale) AS locale,
    CAST(MAX(mozfun.norm.extract_version(browser_version, "major")) AS INT64) AS browser_version,
    LOGICAL_OR(pocket_enabled) AS stories_enabled,
    LOGICAL_OR(pocket_sponsored_stories_enabled) AS sponsored_stories_enabled,
    LOGICAL_OR(topsites_enabled) AS topsites_enabled,
    LOGICAL_OR(topsites_sponsored_enabled) AS sponsored_topsites_enabled,
    LOGICAL_OR(topic_preferences_set) AS topic_preferences_set,
    SUM(newtab_visit_count) AS newtab_visit_count,
    SUM(visits_with_non_search_engagement) AS visits_with_non_search_engagement,
    SUM(organic_topsite_tile_clicks) AS organic_topsite_tile_clicks,
    SUM(sponsored_topsite_tile_clicks) AS sponsored_topsite_tile_clicks,
    SUM(organic_topsite_tile_impressions) AS organic_topsite_tile_impressions,
    SUM(sponsored_topsite_tile_impressions) AS sponsored_topsite_tile_impressions,
    SUM(organic_topsite_tile_dismissals) AS organic_topsite_tile_dismissals,
    SUM(sponsored_topsite_tile_dismissals) AS sponsored_topsite_tile_dismissals,
    SUM(organic_pocket_impressions) AS organic_stories_impressions,
    SUM(sponsored_pocket_impressions) AS sponsored_stories_impressions,
    SUM(organic_pocket_clicks) AS organic_stories_clicks,
    SUM(sponsored_pocket_clicks) AS sponsored_stories_clicks,
    SUM(organic_pocket_dismissals) AS organic_stories_dismissals,
    SUM(sponsored_pocket_dismissals) AS sponsored_stories_dismissals,
  FROM
    `moz-fx-data-shared-prod.telemetry_derived.newtab_clients_daily_v1`
  WHERE
    submission_date = @submission_date
  GROUP BY
    submission_date,
    client_id
),
joined AS (
  SELECT
    submission_date,
    client_id,
    DATE_TRUNC(s.profile_creation_date, MONTH) AS profile_creation_month,
    COALESCE(s.app_version, nt.browser_version) AS app_version,
    COALESCE(s.country, nt.country_code) AS country,
    COALESCE(s.os, nt.normalized_os) AS normalized_os,
    COALESCE(s.channel, nt.channel) AS channel,
    COALESCE(s.locale, nt.locale) AS locale,
    s.client_id IS NOT NULL
    AND nt.client_id IS NOT NULL AS client_with_newtab_and_search_activity,
    COALESCE(s.ad_click, 0) + COALESCE(
      nt.sponsored_topsite_tile_clicks + nt.sponsored_stories_clicks,
      0
    ) > 0 AS client_with_ad_engagement,
    COALESCE(s.ad_click, 0) > 0 AS client_with_search_ad_engagement,
    COALESCE(
      nt.sponsored_topsite_tile_clicks + nt.sponsored_stories_clicks,
      0
    ) > 0 AS client_with_newtab_ad_engagement,
    s.default_search_engine,
    s.is_default_browser,
    s.is_sap_monetizable,
    s.has_adblocker_addon,
    s.policies_is_enterprise,
    nt.stories_enabled,
    nt.sponsored_stories_enabled,
    nt.topsites_enabled,
    nt.sponsored_topsites_enabled,
    nt.topic_preferences_set,
    COALESCE(s.subsession_hours_sum, 0) AS subsession_hours_sum,
    COALESCE(s.sessions_started_on_this_day, 0) AS sessions_started_on_this_day,
    COALESCE(s.active_addons_count_mean, 0) AS active_addons_count_mean,
    COALESCE(s.max_concurrent_tab_count_max, 0) AS max_concurrent_tab_count_max,
    COALESCE(s.active_hours_sum, 0) AS active_hours_sum,
    COALESCE(s.total_uri_count, 0) AS total_uri_count,
    COALESCE(s.organic, 0) AS organic,
    COALESCE(s.tagged_sap, 0) AS tagged_sap,
    COALESCE(s.tagged_follow_on, 0) AS tagged_follow_on,
    COALESCE(s.sap, 0) AS sap,
    COALESCE(s.ad_click, 0) AS ad_click,
    COALESCE(s.ad_click_organic, 0) AS ad_click_organic,
    COALESCE(s.search_with_ads, 0) AS search_with_ads,
    COALESCE(s.search_with_ads_organic, 0) AS search_with_ads_organic,
    COALESCE(nt.newtab_visit_count, 0) AS newtab_visit_count,
    COALESCE(nt.visits_with_non_search_engagement, 0) AS visits_with_non_search_engagement,
    COALESCE(nt.organic_topsite_tile_clicks, 0) AS organic_topsite_tile_clicks,
    COALESCE(nt.sponsored_topsite_tile_clicks, 0) AS sponsored_topsite_tile_clicks,
    COALESCE(nt.organic_topsite_tile_impressions, 0) AS organic_topsite_tile_impressions,
    COALESCE(nt.sponsored_topsite_tile_impressions, 0) AS sponsored_topsite_tile_impressions,
    COALESCE(nt.organic_topsite_tile_dismissals, 0) AS organic_topsite_tile_dismissals,
    COALESCE(nt.sponsored_topsite_tile_dismissals, 0) AS sponsored_topsite_tile_dismissals,
    COALESCE(nt.organic_stories_impressions, 0) AS organic_stories_impressions,
    COALESCE(nt.sponsored_stories_impressions, 0) AS sponsored_stories_impressions,
    COALESCE(nt.organic_stories_clicks, 0) AS organic_stories_clicks,
    COALESCE(nt.sponsored_stories_clicks, 0) AS sponsored_stories_clicks,
    COALESCE(nt.organic_stories_dismissals, 0) AS organic_stories_dismissals,
    COALESCE(nt.sponsored_stories_dismissals, 0) AS sponsored_stories_dismissals,
  FROM
    search_clients_daily s
  FULL JOIN
    newtab_clients_daily nt
    USING (submission_date, client_id)
)
SELECT
  submission_date,
  app_version,
  country,
  normalized_os,
  channel,
  locale,
  profile_creation_month,
  default_search_engine,
  is_default_browser,
  is_sap_monetizable,
  has_adblocker_addon,
  policies_is_enterprise,
  stories_enabled,
  sponsored_stories_enabled,
  topsites_enabled,
  sponsored_topsites_enabled,
  topic_preferences_set,
  COUNT(client_id) AS client_count,
  COUNTIF(client_with_newtab_and_search_activity) AS clients_with_newtab_and_search_activity,
  COUNTIF(client_with_ad_engagement) AS clients_with_ad_engagement,
  COUNTIF(client_with_search_ad_engagement) AS clients_with_search_ad_engagement,
  COUNTIF(client_with_newtab_ad_engagement) AS clients_with_newtab_ad_engagement,
  SUM(subsession_hours_sum) AS subsession_hours_sum_total,
  APPROX_QUANTILES(subsession_hours_sum, 99) AS subsession_hours_sum_histogram,
  SUM(sessions_started_on_this_day) AS sessions_started_on_this_day_total,
  APPROX_QUANTILES(sessions_started_on_this_day, 99) AS sessions_started_on_this_day_histogram,
  APPROX_QUANTILES(active_addons_count_mean, 99) AS active_addons_count_mean_histogram,
  APPROX_QUANTILES(max_concurrent_tab_count_max, 99) AS max_concurrent_tab_count_max_histogram,
  APPROX_QUANTILES(active_hours_sum, 99) AS active_hours_sum_histogram,
  APPROX_QUANTILES(total_uri_count, 99) AS total_uri_count_histogram,
  SUM(organic) AS organic_sum,
  APPROX_QUANTILES(organic, 99) AS organic_histogram,
  SUM(tagged_sap) AS tagged_sap_sum,
  APPROX_QUANTILES(tagged_sap, 99) AS tagged_sap_histogram,
  SUM(tagged_follow_on) AS tagged_follow_on_sum,
  APPROX_QUANTILES(tagged_follow_on, 99) AS tagged_follow_on_histogram,
  SUM(sap) AS sap_sum,
  APPROX_QUANTILES(sap, 99) AS sap_histogram,
  SUM(ad_click) AS ad_click_sum,
  APPROX_QUANTILES(ad_click, 99) AS ad_click_histogram,
  SUM(ad_click_organic) AS ad_click_organic_sum,
  APPROX_QUANTILES(ad_click_organic, 99) AS ad_click_organic_histogram,
  SUM(search_with_ads) AS search_with_ads_sum,
  APPROX_QUANTILES(search_with_ads, 99) AS search_with_ads_histogram,
  SUM(newtab_visit_count) AS newtab_visit_count_sum,
  APPROX_QUANTILES(newtab_visit_count, 99) AS newtab_visit_count_histogram,
  SUM(visits_with_non_search_engagement) AS visits_with_non_search_engagement_sum,
  APPROX_QUANTILES(
    visits_with_non_search_engagement,
    99
  ) AS visits_with_non_search_engagement_histogram,
  SUM(organic_topsite_tile_clicks) AS organic_topsite_tile_clicks_sum,
  APPROX_QUANTILES(organic_topsite_tile_clicks, 99) AS organic_topsite_tile_clicks_histogram,
  SUM(sponsored_topsite_tile_clicks) AS sponsored_topsite_tile_clicks_sum,
  APPROX_QUANTILES(sponsored_topsite_tile_clicks, 99) AS sponsored_topsite_tile_clicks_histogram,
  SUM(organic_topsite_tile_impressions) AS organic_topsite_tile_impressions_sum,
  APPROX_QUANTILES(
    organic_topsite_tile_impressions,
    99
  ) AS organic_topsite_tile_impressions_histogram,
  SUM(sponsored_topsite_tile_impressions) AS sponsored_topsite_tile_impressions_sum,
  APPROX_QUANTILES(
    sponsored_topsite_tile_impressions,
    99
  ) AS sponsored_topsite_tile_impressions_histogram,
  SUM(organic_topsite_tile_dismissals) AS organic_topsite_tile_dismissals_sum,
  APPROX_QUANTILES(
    organic_topsite_tile_dismissals,
    99
  ) AS organic_topsite_tile_dismissals_histogram,
  SUM(sponsored_topsite_tile_dismissals) AS sponsored_topsite_tile_dismissals_sum,
  APPROX_QUANTILES(
    sponsored_topsite_tile_dismissals,
    99
  ) AS sponsored_topsite_tile_dismissals_histogram,
  SUM(organic_stories_impressions) AS organic_stories_impressions_sum,
  APPROX_QUANTILES(organic_stories_impressions, 99) AS organic_stories_impressions_histogram,
  SUM(sponsored_stories_impressions) AS sponsored_stories_impressions_sum,
  APPROX_QUANTILES(sponsored_stories_impressions, 99) AS sponsored_stories_impressions_histogram,
  SUM(organic_stories_clicks) AS organic_stories_clicks_sum,
  APPROX_QUANTILES(organic_stories_clicks, 99) AS organic_stories_clicks_histogram,
  SUM(sponsored_stories_clicks) AS sponsored_stories_clicks_sum,
  APPROX_QUANTILES(sponsored_stories_clicks, 99) AS sponsored_stories_clicks_histogram,
  SUM(organic_stories_dismissals) AS organic_stories_dismissals_sum,
  APPROX_QUANTILES(organic_stories_dismissals, 99) AS organic_stories_dismissals_histogram,
  SUM(sponsored_stories_dismissals) AS sponsored_stories_dismissals_sum,
  APPROX_QUANTILES(sponsored_stories_dismissals, 99) AS sponsored_stories_dismissals_histogram
FROM
  joined
GROUP BY
  submission_date,
  app_version,
  country,
  normalized_os,
  channel,
  locale,
  profile_creation_month,
  default_search_engine,
  is_default_browser,
  is_sap_monetizable,
  has_adblocker_addon,
  policies_is_enterprise,
  stories_enabled,
  sponsored_stories_enabled,
  topsites_enabled,
  sponsored_topsites_enabled,
  topic_preferences_set
