-- Query generated by: python3 -m bigquery_etl.glam.clients_daily_scalar_aggregates --source-table org_mozilla_fenix_stable.migration_v1
WITH extracted AS (
  SELECT
    *,
    DATE(submission_timestamp) AS submission_date,
    client_info.client_id,
    "migration" AS ping_type,
    COALESCE(
      SAFE_CAST(SPLIT(client_info.app_display_version, '.')[OFFSET(0)] AS INT64),
      0
    ) AS app_version,
    client_info.os AS os,
    client_info.app_build AS app_build_id,
    client_info.app_channel AS channel
  FROM
    `moz-fx-data-shared-prod.org_mozilla_fenix_stable.migration_v1`
  WHERE
    DATE(submission_timestamp) = @submission_date
    AND client_info.client_id IS NOT NULL
),
unlabeled_metrics AS (
  SELECT
    client_id,
    ping_type,
    submission_date,
    os,
    app_version,
    app_build_id,
    channel,
    ARRAY<STRUCT<metric STRING, metric_type STRING, key STRING, agg_type STRING, value FLOAT64>>[
      (
        'migration_addons_any_failures',
        'boolean',
        '',
        'false',
        SUM(CAST(NOT metrics.boolean.migration_addons_any_failures AS INT64))
      ),
      (
        'migration_addons_any_failures',
        'boolean',
        '',
        'true',
        SUM(CAST(metrics.boolean.migration_addons_any_failures AS INT64))
      ),
      (
        'migration_addons_failed_addons',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_addons_failed_addons AS INT64))
      ),
      (
        'migration_addons_failed_addons',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_addons_failed_addons) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_addons_failed_addons',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_addons_failed_addons AS INT64))
      ),
      (
        'migration_addons_failed_addons',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_addons_failed_addons AS INT64))
      ),
      (
        'migration_addons_failed_addons',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_addons_failed_addons AS INT64))
      ),
      (
        'migration_addons_failure_reason',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_addons_failure_reason AS INT64))
      ),
      (
        'migration_addons_failure_reason',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_addons_failure_reason) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_addons_failure_reason',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_addons_failure_reason AS INT64))
      ),
      (
        'migration_addons_failure_reason',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_addons_failure_reason AS INT64))
      ),
      (
        'migration_addons_failure_reason',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_addons_failure_reason AS INT64))
      ),
      (
        'migration_addons_migrated_addons',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_addons_migrated_addons AS INT64))
      ),
      (
        'migration_addons_migrated_addons',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_addons_migrated_addons) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_addons_migrated_addons',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_addons_migrated_addons AS INT64))
      ),
      (
        'migration_addons_migrated_addons',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_addons_migrated_addons AS INT64))
      ),
      (
        'migration_addons_migrated_addons',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_addons_migrated_addons AS INT64))
      ),
      (
        'migration_addons_success_reason',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_addons_success_reason AS INT64))
      ),
      (
        'migration_addons_success_reason',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_addons_success_reason) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_addons_success_reason',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_addons_success_reason AS INT64))
      ),
      (
        'migration_addons_success_reason',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_addons_success_reason AS INT64))
      ),
      (
        'migration_addons_success_reason',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_addons_success_reason AS INT64))
      ),
      (
        'migration_bookmarks_any_failures',
        'boolean',
        '',
        'false',
        SUM(CAST(NOT metrics.boolean.migration_bookmarks_any_failures AS INT64))
      ),
      (
        'migration_bookmarks_any_failures',
        'boolean',
        '',
        'true',
        SUM(CAST(metrics.boolean.migration_bookmarks_any_failures AS INT64))
      ),
      (
        'migration_bookmarks_detected',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_bookmarks_detected AS INT64))
      ),
      (
        'migration_bookmarks_detected',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_bookmarks_detected) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_bookmarks_detected',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_bookmarks_detected AS INT64))
      ),
      (
        'migration_bookmarks_detected',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_bookmarks_detected AS INT64))
      ),
      (
        'migration_bookmarks_detected',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_bookmarks_detected AS INT64))
      ),
      (
        'migration_bookmarks_failure_reason',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_bookmarks_failure_reason AS INT64))
      ),
      (
        'migration_bookmarks_failure_reason',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_bookmarks_failure_reason) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_bookmarks_failure_reason',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_bookmarks_failure_reason AS INT64))
      ),
      (
        'migration_bookmarks_failure_reason',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_bookmarks_failure_reason AS INT64))
      ),
      (
        'migration_bookmarks_failure_reason',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_bookmarks_failure_reason AS INT64))
      ),
      (
        'migration_bookmarks_success_reason',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_bookmarks_success_reason AS INT64))
      ),
      (
        'migration_bookmarks_success_reason',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_bookmarks_success_reason) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_bookmarks_success_reason',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_bookmarks_success_reason AS INT64))
      ),
      (
        'migration_bookmarks_success_reason',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_bookmarks_success_reason AS INT64))
      ),
      (
        'migration_bookmarks_success_reason',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_bookmarks_success_reason AS INT64))
      ),
      (
        'migration_fxa_any_failures',
        'boolean',
        '',
        'false',
        SUM(CAST(NOT metrics.boolean.migration_fxa_any_failures AS INT64))
      ),
      (
        'migration_fxa_any_failures',
        'boolean',
        '',
        'true',
        SUM(CAST(metrics.boolean.migration_fxa_any_failures AS INT64))
      ),
      (
        'migration_fxa_failure_reason',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_fxa_failure_reason AS INT64))
      ),
      (
        'migration_fxa_failure_reason',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_fxa_failure_reason) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_fxa_failure_reason',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_fxa_failure_reason AS INT64))
      ),
      (
        'migration_fxa_failure_reason',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_fxa_failure_reason AS INT64))
      ),
      (
        'migration_fxa_failure_reason',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_fxa_failure_reason AS INT64))
      ),
      (
        'migration_fxa_has_custom_idp_server',
        'boolean',
        '',
        'false',
        SUM(CAST(NOT metrics.boolean.migration_fxa_has_custom_idp_server AS INT64))
      ),
      (
        'migration_fxa_has_custom_idp_server',
        'boolean',
        '',
        'true',
        SUM(CAST(metrics.boolean.migration_fxa_has_custom_idp_server AS INT64))
      ),
      (
        'migration_fxa_has_custom_token_server',
        'boolean',
        '',
        'false',
        SUM(CAST(NOT metrics.boolean.migration_fxa_has_custom_token_server AS INT64))
      ),
      (
        'migration_fxa_has_custom_token_server',
        'boolean',
        '',
        'true',
        SUM(CAST(metrics.boolean.migration_fxa_has_custom_token_server AS INT64))
      ),
      (
        'migration_fxa_success_reason',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_fxa_success_reason AS INT64))
      ),
      (
        'migration_fxa_success_reason',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_fxa_success_reason) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_fxa_success_reason',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_fxa_success_reason AS INT64))
      ),
      (
        'migration_fxa_success_reason',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_fxa_success_reason AS INT64))
      ),
      (
        'migration_fxa_success_reason',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_fxa_success_reason AS INT64))
      ),
      (
        'migration_gecko_any_failures',
        'boolean',
        '',
        'false',
        SUM(CAST(NOT metrics.boolean.migration_gecko_any_failures AS INT64))
      ),
      (
        'migration_gecko_any_failures',
        'boolean',
        '',
        'true',
        SUM(CAST(metrics.boolean.migration_gecko_any_failures AS INT64))
      ),
      (
        'migration_gecko_failure_reason',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_gecko_failure_reason AS INT64))
      ),
      (
        'migration_gecko_failure_reason',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_gecko_failure_reason) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_gecko_failure_reason',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_gecko_failure_reason AS INT64))
      ),
      (
        'migration_gecko_failure_reason',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_gecko_failure_reason AS INT64))
      ),
      (
        'migration_gecko_failure_reason',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_gecko_failure_reason AS INT64))
      ),
      (
        'migration_gecko_success_reason',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_gecko_success_reason AS INT64))
      ),
      (
        'migration_gecko_success_reason',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_gecko_success_reason) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_gecko_success_reason',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_gecko_success_reason AS INT64))
      ),
      (
        'migration_gecko_success_reason',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_gecko_success_reason AS INT64))
      ),
      (
        'migration_gecko_success_reason',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_gecko_success_reason AS INT64))
      ),
      (
        'migration_history_any_failures',
        'boolean',
        '',
        'false',
        SUM(CAST(NOT metrics.boolean.migration_history_any_failures AS INT64))
      ),
      (
        'migration_history_any_failures',
        'boolean',
        '',
        'true',
        SUM(CAST(metrics.boolean.migration_history_any_failures AS INT64))
      ),
      (
        'migration_history_detected',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_history_detected AS INT64))
      ),
      (
        'migration_history_detected',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_history_detected) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_history_detected',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_history_detected AS INT64))
      ),
      (
        'migration_history_detected',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_history_detected AS INT64))
      ),
      (
        'migration_history_detected',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_history_detected AS INT64))
      ),
      (
        'migration_history_failure_reason',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_history_failure_reason AS INT64))
      ),
      (
        'migration_history_failure_reason',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_history_failure_reason) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_history_failure_reason',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_history_failure_reason AS INT64))
      ),
      (
        'migration_history_failure_reason',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_history_failure_reason AS INT64))
      ),
      (
        'migration_history_failure_reason',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_history_failure_reason AS INT64))
      ),
      (
        'migration_history_success_reason',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_history_success_reason AS INT64))
      ),
      (
        'migration_history_success_reason',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_history_success_reason) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_history_success_reason',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_history_success_reason AS INT64))
      ),
      (
        'migration_history_success_reason',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_history_success_reason AS INT64))
      ),
      (
        'migration_history_success_reason',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_history_success_reason AS INT64))
      ),
      (
        'migration_logins_any_failures',
        'boolean',
        '',
        'false',
        SUM(CAST(NOT metrics.boolean.migration_logins_any_failures AS INT64))
      ),
      (
        'migration_logins_any_failures',
        'boolean',
        '',
        'true',
        SUM(CAST(metrics.boolean.migration_logins_any_failures AS INT64))
      ),
      (
        'migration_logins_detected',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_logins_detected AS INT64))
      ),
      (
        'migration_logins_detected',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_logins_detected) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_logins_detected',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_logins_detected AS INT64))
      ),
      (
        'migration_logins_detected',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_logins_detected AS INT64))
      ),
      (
        'migration_logins_detected',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_logins_detected AS INT64))
      ),
      (
        'migration_logins_failure_reason',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_logins_failure_reason AS INT64))
      ),
      (
        'migration_logins_failure_reason',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_logins_failure_reason) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_logins_failure_reason',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_logins_failure_reason AS INT64))
      ),
      (
        'migration_logins_failure_reason',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_logins_failure_reason AS INT64))
      ),
      (
        'migration_logins_failure_reason',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_logins_failure_reason AS INT64))
      ),
      (
        'migration_logins_success_reason',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_logins_success_reason AS INT64))
      ),
      (
        'migration_logins_success_reason',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_logins_success_reason) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_logins_success_reason',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_logins_success_reason AS INT64))
      ),
      (
        'migration_logins_success_reason',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_logins_success_reason AS INT64))
      ),
      (
        'migration_logins_success_reason',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_logins_success_reason AS INT64))
      ),
      (
        'migration_logins_unsupported_db_version',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_logins_unsupported_db_version AS INT64))
      ),
      (
        'migration_logins_unsupported_db_version',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_logins_unsupported_db_version) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_logins_unsupported_db_version',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_logins_unsupported_db_version AS INT64))
      ),
      (
        'migration_logins_unsupported_db_version',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_logins_unsupported_db_version AS INT64))
      ),
      (
        'migration_logins_unsupported_db_version',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_logins_unsupported_db_version AS INT64))
      ),
      (
        'migration_open_tabs_any_failures',
        'boolean',
        '',
        'false',
        SUM(CAST(NOT metrics.boolean.migration_open_tabs_any_failures AS INT64))
      ),
      (
        'migration_open_tabs_any_failures',
        'boolean',
        '',
        'true',
        SUM(CAST(metrics.boolean.migration_open_tabs_any_failures AS INT64))
      ),
      (
        'migration_open_tabs_detected',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_open_tabs_detected AS INT64))
      ),
      (
        'migration_open_tabs_detected',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_open_tabs_detected) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_open_tabs_detected',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_open_tabs_detected AS INT64))
      ),
      (
        'migration_open_tabs_detected',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_open_tabs_detected AS INT64))
      ),
      (
        'migration_open_tabs_detected',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_open_tabs_detected AS INT64))
      ),
      (
        'migration_open_tabs_failure_reason',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_open_tabs_failure_reason AS INT64))
      ),
      (
        'migration_open_tabs_failure_reason',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_open_tabs_failure_reason) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_open_tabs_failure_reason',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_open_tabs_failure_reason AS INT64))
      ),
      (
        'migration_open_tabs_failure_reason',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_open_tabs_failure_reason AS INT64))
      ),
      (
        'migration_open_tabs_failure_reason',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_open_tabs_failure_reason AS INT64))
      ),
      (
        'migration_open_tabs_migrated',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_open_tabs_migrated AS INT64))
      ),
      (
        'migration_open_tabs_migrated',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_open_tabs_migrated) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_open_tabs_migrated',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_open_tabs_migrated AS INT64))
      ),
      (
        'migration_open_tabs_migrated',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_open_tabs_migrated AS INT64))
      ),
      (
        'migration_open_tabs_migrated',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_open_tabs_migrated AS INT64))
      ),
      (
        'migration_open_tabs_success_reason',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_open_tabs_success_reason AS INT64))
      ),
      (
        'migration_open_tabs_success_reason',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_open_tabs_success_reason) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_open_tabs_success_reason',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_open_tabs_success_reason AS INT64))
      ),
      (
        'migration_open_tabs_success_reason',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_open_tabs_success_reason AS INT64))
      ),
      (
        'migration_open_tabs_success_reason',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_open_tabs_success_reason AS INT64))
      ),
      (
        'migration_pinned_sites_any_failures',
        'boolean',
        '',
        'false',
        SUM(CAST(NOT metrics.boolean.migration_pinned_sites_any_failures AS INT64))
      ),
      (
        'migration_pinned_sites_any_failures',
        'boolean',
        '',
        'true',
        SUM(CAST(metrics.boolean.migration_pinned_sites_any_failures AS INT64))
      ),
      (
        'migration_pinned_sites_detected_pinned_sites',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_pinned_sites_detected_pinned_sites AS INT64))
      ),
      (
        'migration_pinned_sites_detected_pinned_sites',
        'counter',
        '',
        'count',
        IF(
          MIN(metrics.counter.migration_pinned_sites_detected_pinned_sites) IS NULL,
          NULL,
          COUNT(*)
        )
      ),
      (
        'migration_pinned_sites_detected_pinned_sites',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_pinned_sites_detected_pinned_sites AS INT64))
      ),
      (
        'migration_pinned_sites_detected_pinned_sites',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_pinned_sites_detected_pinned_sites AS INT64))
      ),
      (
        'migration_pinned_sites_detected_pinned_sites',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_pinned_sites_detected_pinned_sites AS INT64))
      ),
      (
        'migration_pinned_sites_failure_reason',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_pinned_sites_failure_reason AS INT64))
      ),
      (
        'migration_pinned_sites_failure_reason',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_pinned_sites_failure_reason) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_pinned_sites_failure_reason',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_pinned_sites_failure_reason AS INT64))
      ),
      (
        'migration_pinned_sites_failure_reason',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_pinned_sites_failure_reason AS INT64))
      ),
      (
        'migration_pinned_sites_failure_reason',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_pinned_sites_failure_reason AS INT64))
      ),
      (
        'migration_pinned_sites_migrated_pinned_sites',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_pinned_sites_migrated_pinned_sites AS INT64))
      ),
      (
        'migration_pinned_sites_migrated_pinned_sites',
        'counter',
        '',
        'count',
        IF(
          MIN(metrics.counter.migration_pinned_sites_migrated_pinned_sites) IS NULL,
          NULL,
          COUNT(*)
        )
      ),
      (
        'migration_pinned_sites_migrated_pinned_sites',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_pinned_sites_migrated_pinned_sites AS INT64))
      ),
      (
        'migration_pinned_sites_migrated_pinned_sites',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_pinned_sites_migrated_pinned_sites AS INT64))
      ),
      (
        'migration_pinned_sites_migrated_pinned_sites',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_pinned_sites_migrated_pinned_sites AS INT64))
      ),
      (
        'migration_pinned_sites_success_reason',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_pinned_sites_success_reason AS INT64))
      ),
      (
        'migration_pinned_sites_success_reason',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_pinned_sites_success_reason) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_pinned_sites_success_reason',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_pinned_sites_success_reason AS INT64))
      ),
      (
        'migration_pinned_sites_success_reason',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_pinned_sites_success_reason AS INT64))
      ),
      (
        'migration_pinned_sites_success_reason',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_pinned_sites_success_reason AS INT64))
      ),
      (
        'migration_search_any_failures',
        'boolean',
        '',
        'false',
        SUM(CAST(NOT metrics.boolean.migration_search_any_failures AS INT64))
      ),
      (
        'migration_search_any_failures',
        'boolean',
        '',
        'true',
        SUM(CAST(metrics.boolean.migration_search_any_failures AS INT64))
      ),
      (
        'migration_search_failure_reason',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_search_failure_reason AS INT64))
      ),
      (
        'migration_search_failure_reason',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_search_failure_reason) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_search_failure_reason',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_search_failure_reason AS INT64))
      ),
      (
        'migration_search_failure_reason',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_search_failure_reason AS INT64))
      ),
      (
        'migration_search_failure_reason',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_search_failure_reason AS INT64))
      ),
      (
        'migration_search_success_reason',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_search_success_reason AS INT64))
      ),
      (
        'migration_search_success_reason',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_search_success_reason) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_search_success_reason',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_search_success_reason AS INT64))
      ),
      (
        'migration_search_success_reason',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_search_success_reason AS INT64))
      ),
      (
        'migration_search_success_reason',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_search_success_reason AS INT64))
      ),
      (
        'migration_settings_any_failures',
        'boolean',
        '',
        'false',
        SUM(CAST(NOT metrics.boolean.migration_settings_any_failures AS INT64))
      ),
      (
        'migration_settings_any_failures',
        'boolean',
        '',
        'true',
        SUM(CAST(metrics.boolean.migration_settings_any_failures AS INT64))
      ),
      (
        'migration_settings_failure_reason',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_settings_failure_reason AS INT64))
      ),
      (
        'migration_settings_failure_reason',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_settings_failure_reason) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_settings_failure_reason',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_settings_failure_reason AS INT64))
      ),
      (
        'migration_settings_failure_reason',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_settings_failure_reason AS INT64))
      ),
      (
        'migration_settings_failure_reason',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_settings_failure_reason AS INT64))
      ),
      (
        'migration_settings_success_reason',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_settings_success_reason AS INT64))
      ),
      (
        'migration_settings_success_reason',
        'counter',
        '',
        'count',
        IF(MIN(metrics.counter.migration_settings_success_reason) IS NULL, NULL, COUNT(*))
      ),
      (
        'migration_settings_success_reason',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_settings_success_reason AS INT64))
      ),
      (
        'migration_settings_success_reason',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_settings_success_reason AS INT64))
      ),
      (
        'migration_settings_success_reason',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_settings_success_reason AS INT64))
      ),
      (
        'migration_settings_telemetry_enabled',
        'boolean',
        '',
        'false',
        SUM(CAST(NOT metrics.boolean.migration_settings_telemetry_enabled AS INT64))
      ),
      (
        'migration_settings_telemetry_enabled',
        'boolean',
        '',
        'true',
        SUM(CAST(metrics.boolean.migration_settings_telemetry_enabled AS INT64))
      ),
      (
        'migration_telemetry_identifiers_any_failures',
        'boolean',
        '',
        'false',
        SUM(CAST(NOT metrics.boolean.migration_telemetry_identifiers_any_failures AS INT64))
      ),
      (
        'migration_telemetry_identifiers_any_failures',
        'boolean',
        '',
        'true',
        SUM(CAST(metrics.boolean.migration_telemetry_identifiers_any_failures AS INT64))
      ),
      (
        'migration_telemetry_identifiers_failure_reason',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_telemetry_identifiers_failure_reason AS INT64))
      ),
      (
        'migration_telemetry_identifiers_failure_reason',
        'counter',
        '',
        'count',
        IF(
          MIN(metrics.counter.migration_telemetry_identifiers_failure_reason) IS NULL,
          NULL,
          COUNT(*)
        )
      ),
      (
        'migration_telemetry_identifiers_failure_reason',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_telemetry_identifiers_failure_reason AS INT64))
      ),
      (
        'migration_telemetry_identifiers_failure_reason',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_telemetry_identifiers_failure_reason AS INT64))
      ),
      (
        'migration_telemetry_identifiers_failure_reason',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_telemetry_identifiers_failure_reason AS INT64))
      ),
      (
        'migration_telemetry_identifiers_success_reason',
        'counter',
        '',
        'avg',
        avg(CAST(metrics.counter.migration_telemetry_identifiers_success_reason AS INT64))
      ),
      (
        'migration_telemetry_identifiers_success_reason',
        'counter',
        '',
        'count',
        IF(
          MIN(metrics.counter.migration_telemetry_identifiers_success_reason) IS NULL,
          NULL,
          COUNT(*)
        )
      ),
      (
        'migration_telemetry_identifiers_success_reason',
        'counter',
        '',
        'max',
        max(CAST(metrics.counter.migration_telemetry_identifiers_success_reason AS INT64))
      ),
      (
        'migration_telemetry_identifiers_success_reason',
        'counter',
        '',
        'min',
        min(CAST(metrics.counter.migration_telemetry_identifiers_success_reason AS INT64))
      ),
      (
        'migration_telemetry_identifiers_success_reason',
        'counter',
        '',
        'sum',
        sum(CAST(metrics.counter.migration_telemetry_identifiers_success_reason AS INT64))
      )
    ] AS scalar_aggregates
  FROM
    extracted
  GROUP BY
    client_id,
    ping_type,
    submission_date,
    os,
    app_version,
    app_build_id,
    channel
),
grouped_labeled_metrics AS (
  SELECT
    client_id,
    ping_type,
    submission_date,
    os,
    app_version,
    app_build_id,
    channel,
    ARRAY<STRUCT<name STRING, type STRING, value ARRAY<STRUCT<key STRING, value INT64>>>>[
      (
        'glean_error_invalid_label',
        'labeled_counter',
        metrics.labeled_counter.glean_error_invalid_label
      ),
      (
        'glean_error_invalid_overflow',
        'labeled_counter',
        metrics.labeled_counter.glean_error_invalid_overflow
      ),
      (
        'glean_error_invalid_state',
        'labeled_counter',
        metrics.labeled_counter.glean_error_invalid_state
      ),
      (
        'glean_error_invalid_value',
        'labeled_counter',
        metrics.labeled_counter.glean_error_invalid_value
      ),
      (
        'migration_bookmarks_migrated',
        'labeled_counter',
        metrics.labeled_counter.migration_bookmarks_migrated
      ),
      (
        'migration_history_migrated',
        'labeled_counter',
        metrics.labeled_counter.migration_history_migrated
      ),
      (
        'migration_logins_failure_counts',
        'labeled_counter',
        metrics.labeled_counter.migration_logins_failure_counts
      )
    ] AS metrics
  FROM
    extracted
),
flattened_labeled_metrics AS (
  SELECT
    client_id,
    ping_type,
    submission_date,
    os,
    app_version,
    app_build_id,
    channel,
    metrics.name AS metric,
    metrics.type AS metric_type,
    value.key AS key,
    value.value AS value
  FROM
    grouped_labeled_metrics
  CROSS JOIN
    UNNEST(metrics) AS metrics,
    UNNEST(metrics.value) AS value
),
aggregated_labeled_metrics AS (
  SELECT
    client_id,
    ping_type,
    submission_date,
    os,
    app_version,
    app_build_id,
    channel,
    metric,
    metric_type,
    key,
    MAX(value) AS max,
    MIN(value) AS min,
    AVG(value) AS avg,
    SUM(value) AS sum,
    IF(MIN(value) IS NULL, NULL, COUNT(*)) AS count
  FROM
    flattened_labeled_metrics
  GROUP BY
    client_id,
    ping_type,
    submission_date,
    os,
    app_version,
    app_build_id,
    channel,
    metric,
    metric_type,
    key
),
labeled_metrics AS (
  SELECT
    client_id,
    ping_type,
    submission_date,
    os,
    app_version,
    app_build_id,
    channel,
    ARRAY_CONCAT_AGG(
      ARRAY<STRUCT<metric STRING, metric_type STRING, key STRING, agg_type STRING, value FLOAT64>>[
        (metric, metric_type, key, 'max', max),
        (metric, metric_type, key, 'min', min),
        (metric, metric_type, key, 'avg', avg),
        (metric, metric_type, key, 'sum', sum),
        (metric, metric_type, key, 'count', count)
      ]
    ) AS scalar_aggregates
  FROM
    aggregated_labeled_metrics
  GROUP BY
    client_id,
    ping_type,
    submission_date,
    os,
    app_version,
    app_build_id,
    channel
)
SELECT
  *
FROM
  unlabeled_metrics
UNION ALL
SELECT
  *
FROM
  labeled_metrics
