SQL 코드카타
문제 107. 1789. Primary Department for Each Employee : where, union 활용
LeetCode - The World's Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Employees can belong to multiple departments. When the employee joins other departments, they need to decide which department is their primary department. Note that when an employee belongs to only one department, their primary column is 'N'.
Write a solution to report all the employees with their primary department. For employees who belong to one department, report their only department.
Return the result table in any order.
오답1
select employee_id,
if(count(*)=1, department_id, if(primary_flag = "Y", department_id, Null)) as department_id
from employee
group by employee_id
아래처럼, 4가 null처리가 되어버림.
Output
| employee_id | department_id |
| ----------- | ------------- |
| 1 | 1 |
| 2 | 1 |
| 3 | 3 |
| 4 | null |
Expected
| employee_id | department_id |
| ----------- | ------------- |
| 1 | 1 |
| 2 | 1 |
| 3 | 3 |
| 4 | 3 |
오답2
select employee_id,
case when count(*) = 1 then department_id
when count(*) >= 2 and primary_flag = "Y" then department_id
end as department_id
from employee
group by employee_id
having department_id is not null
employee_id 4가 아예 출력이 안 됨.
이러나 저러나 조건문으로 처리할 수 있는 문제가 아니었다. 나머지의 경우는 어떻게 처리가 되지만, 행에서 조건에 해당하지 않는 값이 먼저 나오는 경우를 처리할 수 있는 방법이 적어도 내가 알고 있는 범위 내에선 없었다.
정답
select employee_id,
department_id
from employee
where primary_flag = 'Y' or employee_id in (select employee_id
from employee
group by employee_id
having count(*)=1)
조건문에만 매달리다가 결국 solution을 슬쩍 보고 풀었던 문제.
요점은 where을 통해 primary_flag가 Y인 경우와 employee_id가 중복되지 않는 경우 각각에 대한 조건을 주는 것이었다.
막혔던 문제이니 만큼 제출은 미뤄두고 두고두고 더 봐야겠다.
덧붙여서 다음번엔 union방식으로도 풀어봐야겠음
포인트
-문제에 조건이 있는 경우 where 활용을 우선 고민해볼 것! 조건이 여러개라도 상관X
문제 108. 610. Triangle Judgement 조건문 사용 논리 문제
LeetCode - The World's Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Report for every three line segments whether they can form a triangle.
Return the result table in any order.
The result format is in the following example.
오답
select if(x+y>z, if(x+z>y, if(z+y>x, "Yes"), "No"), "No") as triangle
from triangle
정답
select x, y, z,
if(x+y <= z, "No", if(x+z <=y, "No", if(z+y <= x, "No", "Yes"))) as triangle
from triangle
복잡한 수식은 없지만 논리적으로 생각해야해서 꽤 재밌었던 문제.
사실 풀면서 논리적으로 사고를 했다기보단, 솔루션의 제목을 보다가 조건문을 쓴다고 하니 if 절로 풀 수 있지 않을까? 그렇다면 이렇게 할 수 있지 않을까? 하다보니 얼떨결에 풀었던 거나 마찬가지라 다시 풀어봐야할듯 하다.
180. Consecutive Numbers : lead, where 활용
LeetCode - The World's Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
오답1
select rank() over (order by num),
id,
num
from logs
group by 1
having count(*)>=3
논리구조가 맞는지와 별개로 쿼리 내 윈도우 함수 결과물로 그룹화를 할 수 없다는 에러메시지가 났다.
오답2
select num as ConsecutiveNums
from (select id,
num,
lag(num, 1) over () as previous_num1,
lag(num, 2) over () as previous_num2
from logs) a
where num = previous_num1 = previous_num2
select distinct num as ConsecutiveNums
from (select id,
num,
lag(num, 1) over () as previous_num1,
lag(num, 2) over () as previous_num2
from logs) a
where (num = previous_num1 and num = previous_num2)
문제의 관건은 where에서 조건으로 and를 걸어준다는 것인데. 사실 왜인지 모르겠음;
내일이라도 질문방에 올려야지.
'TIL 통합 > SQL' 카테고리의 다른 글
INTERSECT, GROUP_CONCAT, 집계함수 (0) | 2024.01.30 |
---|---|
윈도우 함수-집계 행 지정 (0) | 2024.01.29 |
Exchange Seats if(ifnull) 및 case when 조건문 동시 활용, union all (0) | 2024.01.26 |
1. WHERE, UNION, DISTINCT, 2. WITH, 윈도우함수 (0) | 2024.01.25 |
1/21 - WHERE IN, HAVING 활용 (0) | 2024.01.23 |