ソースコード
with 
H as(--HIGHT
select
AGE
,PF_CODE
,case GENDER_CODE when 20 then 'MALE' else 'FEMALE' end GENDER
,AVERAGE_VALUE as H_AVG
from SCHOOL_HEALTH SH
where CATEGORY_CODE =10 and SURVEY_YEAR=2019
order by AGE desc,GENDER asc
)
,HA as(
select
AGE
,GENDER
,round(avg(H_AVG),1) as H_AVGALL
from H
group by AGE,GENDER
order by AGE desc,GENDER asc
)
,HB as(
select
H.AGE
,H.PF_CODE
,H.GENDER
,H.H_AVG
,1 as NUM1
,case when H_AVG>=H_AVGALL then 1 else 0 end as FLG1
from H
    inner join HA on H.AGE=HA.AGE and H.GENDER=HA.GENDER
)
,W as( --WIGHT
select
AGE
,PF_CODE
,case GENDER_CODE when 20 then 'MALE' else 'FEMALE' end GENDER
,AVERAGE_VALUE as W_AVG
from SCHOOL_HEALTH SH
where CATEGORY_CODE =20 and SURVEY_YEAR=2019
order by AGE desc,GENDER asc
)
,WA as(
select
AGE
,GENDER
,round(avg(W_AVG),1) as W_AVGALL
from W
group by AGE,GENDER
order by AGE desc,GENDER asc
)
,WB as(
select
W.AGE
,W.PF_CODE
,W.GENDER
,W.W_AVG
,1 as NUM2
,case when W_AVG>=W_AVGALL then 1 else 0 end as FLG2
from W
    inner join WA on W.AGE=WA.AGE and W.GENDER=WA.GENDER
)

select 
HB.AGE
,HB.GENDER
,round(avg(H_AVG),1) as H_AVG
,round(cast(sum(FLG1) as real)*100/cast(sum(NUM1) as real),1) ||'%' as H_PER
,round(avg(W_AVG),1) as W_AVG
,round(cast(sum(FLG2) as real)*100/cast(sum(NUM2) as real),1) ||'%' as W_PER

from HB
    inner join WB on HB.AGE=WB.AGE and HB.GENDER=WB.GENDER
group by HB.AGE,HB.GENDER
order by HB.AGE desc,HB.GENDER asc
提出情報
提出日時2023/10/04 11:57:06
コンテスト第8回 SQLコンテスト
問題身長と体重
受験者abcdefghijklmnopqrstuvwxy
状態 (詳細)AC
(Accepted: 正答)
メモリ使用量82 MB
メッセージ
テストケース(通過数/総数)
2/2
状態
メモリ使用量
データパターン1
AC
77 MB
データパターン2
AC
82 MB