解答

216

SQL文(クエリ)

SELECT
  gender
  , MAX(name) AS name
  , MAX(birthday) AS birthday
FROM
  (
    SELECT
      CASE gender
        WHEN 1 THEN "男性"
        WHEN 2 THEN "女性"
        ELSE NULL
      END AS gender
      , NTH_VALUE(name, 2) OVER (PARTITION BY gender ORDER BY birthday DESC)
      AS name
      , NTH_VALUE(birthday, 2) OVER (PARTITION BY gender ORDER BY birthday DESC)
      AS birthday
    FROM
      sample.customers
    WHERE
      birthday IS NOT NULL
  )
GROUP BY
  gender

結果テーブル

結果テーブル

サポートページに戻る