본문 바로가기

SQL/LeetCode&HackerRank

[MySQL] Leetcode - 585. Investments in 2016

문제: https://leetcode.com/problems/investments-in-2016/description/?envType=study-plan-v2&envId=top-sql-50

나의 풀이: 

select round(sum(tiv_2016), 2) as tiv_2016
from insurance
where tiv_2015 in (
    select tiv_2015
    from insurance
    group by tiv_2015
    having count(*) > 1)
and (lat, lon) in (
    select lat, lon
    from insurance
    group by lat, lon
    having count(*) = 1)

 

다른 방법: 상호연관서브쿼리 (correlated subquery)

select round(sum(tiv_2016), 2) as tiv_2016
from insurance i1
where tiv_2015 in (
    select tiv_2015
    from insurance i2
    where i2.pid != i1.pid)
and (lat, lon) not in (
    select lat, lon
    from insurance i3
    where i3.pid != i1.pid)

아웅 이런 생각은 어떻게 하는거야~~