문제
- LeetCode SQL 문제
585. Investments in 2016 / Medium
- 문제 내용 : [링크]
내가 작성한 Query
with temp_01 as( select lat, lon from insurance group by lat, lon having count(*) = 1 ), temp_02 as( select tiv_2015 from insurance group by tiv_2015 having count(*) != 1 ) select round(sum(tiv_2016), 2) as tiv_2016 from insurance where tiv_2015 in (select * from temp_02) and (lat, lon) in (select * from temp_01)
tiv_2015
가 한 명 이상의 policyholders와 같아야하고 (즉, unique 하지 않아야한다.) lat
, lon
이 다른 사람과 같으면 안 된다.(즉, unique 해야 한다.)unique한 lat
, lon
값을 구하기 위해 temp_01
에서 lat
, lon
기준 GROUP BY
해주고, 데이터 개수가 1개인 lat
, lon
을 구해준다. (count(*)=1
인 lat
, lon
)
unique 하지 않은 tiv_2015
를 구하기 위해 위에처럼 temp_02
에서 tiv_2015
기준 GROUP BY
해주고, 데이터 개수가 1개가 아닌 tiv_2015
를 구해준다. (count(*) != 1
인 tiv_2015
)
문제에서 구하기를 원하는 sum(tiv_2016)
을 구해주되, WHERE
절 조건에서 tiv_2015
가 temp_02
의 데이터들에 해당하고, (lat
, lon
)이 temp_01
의 데이터들에 해당해야 한다는 내용을 넣어준다.
where tiv_2015 in (select * from temp_02) and (lat, lon) in (select * from temp_01)
round()
함수를 통해 sum(tiv_2016)
을 소수 둘째자리까지 출력해준다.