coding/sql 코딩테스트
[리트코드] Investments in 2016
임이레
2025. 2. 11. 23:05
with sub_calc as (
select lat
,lon
from (
select lat
, lon
, count(*) as cnt
from Insurance
group by 1,2
) as t
where cnt = 1
)
, sub_2015 as (
select tiv_2015
, count(*) as tot
from Insurance
group by 1
having tot >= 2
)
select round(sum(tiv_2016) , 2) as tiv_2016
from Insurance i join sub_calc s on i.lat = s.lat and i.lon = s.lon
where tiv_2015 in (select tiv_2015 from sub_2015)
조건 1 ) 하나 이상의 다른 보험 계약자와 동일한 tiv_2015 값
조건 2) 다른 보험 계약자와 동일한 도시에 위치하지 않음, lat&lon -> 고유해야 한다.
이렇게 서브 테이블 두개를 만들어 원본 테이블에 조건을 추가하여 원하는 데이터를 추출하면 끝!