ソースコード

with avg_ as 
(
  select 
    gender_code 
    ,age 
    ,category_code 
    ,round(avg(average_value),1) as avg_value
  from school_health 
  where survey_year = 2019 
  group by 1,2,3
)
select 
  s.*
from school_health as s 
inner join avg_ as a 
  on s.gender_code = a.gender_code 
  and s.age = a.age 
  and s.category_code = a.category_code 
-- where s.average_value >= a.avg_value 
;


with avg_ as 
(
  select 
    gender_code 
    ,age 
    ,category_code 
    ,round(avg(average_value),1) as avg_value
  from school_health 
  where survey_year = 2019 
  group by 1,2,3
)
,h as 
(
  select 
    s.age as AGE 
    ,case when s.gender_code = '20' then 'MALE'
      when s.gender_code = '30' then 'FEMALE'
      end as GENDER 
    ,case when s.category_code = '10' then a.avg_value
      end as H_AVG 
    ,round(100.0 * sum(case when s.average_value >= a.avg_value then 1 else 0 end) / count(distinct s.pf_code),1) || '%' as H_PER
  from school_health as s 
  inner join avg_ as a 
    on s.gender_code = a.gender_code 
    and s.age = a.age 
    and s.category_code = a.category_code 
  where s.survey_year = 2019
  group by 1,2,3
  having H_AVG is not null 
)
,w as 
(
  select 
    s.age as AGE 
    ,case when s.gender_code = '20' then 'MALE'
      when s.gender_code = '30' then 'FEMALE'
      end as GENDER 
    ,case when s.category_code = '20' then a.avg_value
      end as W_AVG 
    ,round(100.0 * sum(case when s.average_value >= a.avg_value then 1 else 0 end) / count(distinct s.pf_code),1) || '%' as W_PER
  from school_health as s 
  inner join avg_ as a 
    on s.gender_code = a.gender_code 
    and s.age = a.age 
    and s.category_code = a.category_code 
  where s.survey_year = 2019
  group by 1,2,3
  having W_AVG is not null 
)
-- select
--  *
-- from h 
-- inner join w 
--   on h.AGE = w.AGE 
--   and h.GENDER = w.GENDER


select 
  h.AGE 
  ,h.GENDER 
  ,h.H_AVG 
  ,h.H_PER 
  ,w.W_AVG 
  ,w.W_PER
from h 
inner join w 
  on h.AGE = w.AGE 
  and h.GENDER = w.GENDER
order by h.AGE desc, h.GENDER
;
提出情報
提出日時2023/08/21 10:07:42
コンテスト第8回 SQLコンテスト
問題身長と体重
受験者hattsuriboy
状態 (詳細)AC
(Accepted: 正答)
メモリ使用量100 MB
メッセージ
テストケース(通過数/総数)
2/2
状態
メモリ使用量
データパターン1
AC
96 MB
データパターン2
AC
100 MB