ソースコード
with avg_all as 
(
    select
        s.CATEGORY_CODE
        ,s.GENDER_CODE
        ,s.AGE
        ,round(avg(s.AVERAGE_VALUE),1) as avg--全体の平均値
        ,count(1) as num_area --地域数
    from SCHOOL_HEALTH s
    where s.SURVEY_YEAR = 2019
    group by 1,2,3
)
,calc_rate as (
    select
        s.AGE
        ,s.GENDER_CODE
        ,s.CATEGORY_CODE
        ,a.avg
        ,sum(CASE WHEN s.AVERAGE_VALUE >= a.avg then 1 else 0 end)/cast(a.num_area as real) as rate
    from SCHOOL_HEALTH s
    left join avg_all a
        on s.CATEGORY_CODE = a.CATEGORY_CODE
        and s.GENDER_CODE = a.GENDER_CODE
        and s.AGE = a.AGE
    group by 1,2,3
)
select
    cw.AGE
    ,CASE WHEN cw.GENDER_CODE like '20' then 'MALE'
        WHEN cw.GENDER_CODE like '30' then 'FEMALE'
        end as GENDER
    ,ch.AVG as H_AVG
    ,ch.PER as H_PER
    ,cw.AVG as W_AVG
    ,cw.PER as W_PER
FROM (
    select
        c.AGE
        ,c.GENDER_CODE
        ,c.AVG
        ,round(c.rate * 100, 1)||'%' as PER
    from calc_rate c
    where c.CATEGORY_CODE = '10'
) ch
LEFT JOIN (
    select
        c.AGE
        ,c.GENDER_CODE
        ,c.AVG
        ,round(c.rate * 100, 1)||'%' as PER
    from calc_rate c
    where c.CATEGORY_CODE = '20'
) cw
    ON cw.AGE = ch.AGE
    and cw.GENDER_CODE = ch.GENDER_CODE
order by cw.AGE desc,cw.gender_code desc
提出情報
提出日時2023/08/18 20:38:26
コンテスト第8回 SQLコンテスト
問題身長と体重
受験者hiromi_n
状態 (詳細)WA
(Wrong Answer: 誤答)
メモリ使用量81 MB
メッセージ
テストケース(通過数/総数)
1/2
状態
メモリ使用量
データパターン1
AC
78 MB
データパターン2
WA
81 MB