解答

150

SQL文(クエリ)

WITH master AS (
  SELECT
    CASE
      WHEN prefecture IN("東京") THEN "東京"
      WHEN prefecture IN("千葉", "埼玉", "神奈川") THEN "南関東"
      WHEN prefecture IN("栃木", "群馬", "茨城") THEN "北関東"
      ELSE NULL
    END AS region
    , DATE_DIFF("2019-12-31", birthday, YEAR) AS age
  FROM
    sample.customers
  WHERE
    birthday IS NOT NULL
)
SELECT
  region
  , ROUND(AVG(age), 1) AS avg_age
FROM
  master
WHERE
  region IS NOT NULL
GROUP BY
  region
ORDER BY
  2

結果テーブル

結果テーブル

サポートページに戻る