ソースコード
WITH
  AverageValues AS (
    SELECT
      AGE
    , GENDER_CODE
    , CATEGORY_CODE
    , ROUND(AVG(AVERAGE_VALUE), 1) AS OverallAverage
    FROM
      SCHOOL_HEALTH
    WHERE
      SURVEY_YEAR = 2019
    GROUP BY
      AGE
    , GENDER_CODE
    , CATEGORY_CODE
  )
, AboveAverageCounts AS (
    SELECT
      SH.AGE
    , SH.GENDER_CODE
    , SH.CATEGORY_CODE
    , CASE
        WHEN SH.AVERAGE_VALUE >= AV.OverallAverage THEN 1
        ELSE 0
      END AS CountAboveAvg
    FROM
      SCHOOL_HEALTH SH
      JOIN AverageValues AV ON SH.AGE = AV.AGE
      AND SH.GENDER_CODE = AV.GENDER_CODE
      AND SH.CATEGORY_CODE = AV.CATEGORY_CODE
    WHERE
      SH.SURVEY_YEAR = 2019
  )
SELECT
  SH.AGE
, CASE
    WHEN SH.GENDER_CODE = '20' THEN 'MALE'
    ELSE 'FEMALE'
  END AS GENDER
, ROUND(
    AVG(
      CASE
        WHEN SH.CATEGORY_CODE = '10' THEN SH.AVERAGE_VALUE
      END
    )
  , 1
  ) AS H_AVG
, ROUND(
    (
      100.0 * SUM(
        CASE
          WHEN SH.CATEGORY_CODE = '10' THEN AAC.CountAboveAvg
        END
      ) / SUM(
        CASE
          WHEN SH.CATEGORY_CODE = '10' THEN 1
          ELSE 0
        END
      )
    )
  , 1
  ) || '%' AS H_PER
, ROUND(
    AVG(
      CASE
        WHEN SH.CATEGORY_CODE = '20' THEN SH.AVERAGE_VALUE
      END
    )
  , 1
  ) AS W_AVG
, ROUND(
    (
      100.0 * SUM(
        CASE
          WHEN SH.CATEGORY_CODE = '20' THEN AAC.CountAboveAvg
        END
      ) / SUM(
        CASE
          WHEN SH.CATEGORY_CODE = '20' THEN 1
          ELSE 0
        END
      )
    )
  , 1
  ) || '%' AS W_PER
FROM
  SCHOOL_HEALTH SH
  JOIN AboveAverageCounts AAC ON SH.AGE = AAC.AGE
  AND SH.GENDER_CODE = AAC.GENDER_CODE
  AND SH.CATEGORY_CODE = AAC.CATEGORY_CODE
WHERE
  SH.SURVEY_YEAR = 2019
GROUP BY
  SH.AGE
, SH.GENDER_CODE
ORDER BY
  SH.AGE DESC
, SH.GENDER_CODE DESC;
提出情報
提出日時2023/08/19 07:11:06
コンテスト第8回 SQLコンテスト
問題身長と体重
受験者hiraku
状態 (詳細)TLE
(Time Limit Exceeded: 時間制限オーバー)
メモリ使用量84 MB
メッセージ
テストケース(通過数/総数)
0/2
状態
メモリ使用量
データパターン1
WA
77 MB
データパターン2
TLE
84 MB