https://school.programmers.co.kr/learn/courses/30/lessons/301651

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

 

생각 흐름

  • 특정 세대 대장균이 아니라 모든 세대별 대장균에 대해서 데이터를 조회해야 하므로 재귀 쿼리 사용
  • 자식이 없는 대장균을 거르기 위해 where 절 서브쿼리 사용

 

 

with recursive gen_data as (
    select 1 as generation, id, parent_id
    from ECOLI_DATA
    where parent_id is null
    union all
    select generation+1 as generation, b.id, b.parent_id
    from gen_data a inner join ECOLI_DATA b
    on b.parent_id = a.id
)

select count(*) as count, generation
from gen_data
where id not in (
    select distinct parent_id
    from gen_data
    where parent_id is not null
)
group by generation
order by generation

+ Recent posts