ソースコード
with avg_tbl as (
    select
        AGE
        ,GENDER_CODE
        ,CATEGORY_CODE
        ,round(avg(AVERAGE_VALUE),1) total_avg
        ,count(PF_CODE) total_cnt
    from
        SCHOOL_HEALTH
    where 
        SURVEY_YEAR = 2019
    group by 
        AGE
        ,GENDER_CODE
        ,CATEGORY_CODE
)
, cnt_tbl as (
    select 
        a.AGE
        ,a.GENDER_CODE
        ,a.CATEGORY_CODE
        ,max(b.total_avg) as total_avg
        ,cast(max(b.total_cnt) as real) as total_cnt
        ,cast(count(*) as real) as cnt
    from SCHOOL_HEALTH a
        inner join avg_tbl b
        on a.AGE = b.AGE
        and a.GENDER_CODE = b.GENDER_CODE
        and a.CATEGORY_CODE = b.CATEGORY_CODE
    where 
        a.AVERAGE_VALUE >= b.total_avg
    group by
        a.AGE
        ,a.GENDER_CODE
        ,a.CATEGORY_CODE
)
, h_tbl as (
    select
        AGE
        ,case when GENDER_CODE = '20' then 'MALE' else 'FEMALE' end as GENDER
        --,CATEGORY_CODE
        ,total_avg as H_AVG
        ,round(cast(cnt / total_cnt * 100 as real),1) || '%' as H_PER
        
    from cnt_tbl
    where CATEGORY_CODE = '10'
)
, w_tbl as (
    select
        AGE
        ,case when GENDER_CODE = '20' then 'MALE' else 'FEMALE' end as GENDER
        --,CATEGORY_CODE
        ,total_avg as W_AVG
        ,round(cast(cnt / total_cnt * 100 as real),1) || '%' as W_PER
    from cnt_tbl
    where CATEGORY_CODE = '20'
)
,disp_tbl as (
    select 
        h.AGE
        ,h.GENDER
        ,H_AVG
        ,H_PER
        ,W_AVG
        ,W_PER
    from h_tbl as h
        left join w_tbl as w
        on h.AGE = w.AGE
        and h.GENDER = w.GENDER
)
select 
        AGE
        ,GENDER
        ,H_AVG
        ,H_PER
        ,W_AVG
        ,W_PER
from disp_tbl
order by
    AGE desc
    ,GENDER 
提出情報
提出日時2023/08/18 15:09:09
コンテスト第8回 SQLコンテスト
問題身長と体重
受験者sato
状態 (詳細)WA
(Wrong Answer: 誤答)
メモリ使用量104 MB
メッセージ
テストケース(通過数/総数)
1/2
状態
メモリ使用量
データパターン1
AC
97 MB
データパターン2
WA
104 MB