解答

214

SQL文(クエリ)

SELECT
  gender
  , ROUND(AVG(age), 1) AS avg_age
  , MAX(med_age) AS median_age
FROM
  (
    SELECT
      *
      , PERCENTILE_CONT(age, 0.5) OVER (PARTITION BY gender) 
      AS med_age
    FROM
      (
        SELECT
          CASE gender
            WHEN 1 THEN "男性"
            WHEN 2 THEN "女性"
            ELSE NULL
          END AS gender
          , DATE_DIFF("2019-12-31", birthday, YEAR) AS age
        FROM
          sample.customers
        WHERE
          birthday IS NOT NULL
      )
  )
GROUP BY
  gender

結果テーブル

結果テーブル

サポートページに戻る