SQL/문제풀이

[LeetCode] investments-in-2016 📌

응엉잉 2024. 3. 26. 00:32

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

 

1. tiv_2015가 중복

2. lat, lon이 유일

 

2가지 조건을 WHERE절 서브쿼리를 이용해 작성해주면 된다

 

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 -- 해당 tiv_2015가 2번 이상 등장해야 함
)
AND 
WHERE lat, lon IN (
SELECT lat, lon
FROM insurance
GROUP BY lat, lon
HAVING COUNT(*) = 1 -- 해당 lat, lon 조합은 1번 등장해야 함
)