New Companies | HackerRank
Find total number of employees.
www.hackerrank.com
이번 문제는 계층에 따라 테이블이 있는 문제였다.
Sample Input
Company
Table:
Lead_Manager
Table:
Senior_Manager
Table:
Manager
Table:
Employee
Table:
Sample Output
C1 Monika 1 2 1 2
C2 Samantha 1 1 2 2
c1(Monika), c2(samantha)에 따라 각 계층별로 개수를 출력하는 문제이다.
SM2 같은 경우는 senior_manager 테이블에서 끝나기 때문에 Manager와 Employee에는 없다.
그래서 나는 Company 테이블에서부터 LEFT JOIN 을 해주었다.
SELECT c.company_code
, c.founder
, COUNT(DISTINCT l.lead_manager_code)
, COUNT(DISTINCT s.senior_manager_code)
, COUNT(DISTINCT m.manager_code)
, COUNT(DISTINCT e.employee_code)
FROM Company c LEFT JOIN Lead_Manager l ON c.company_code = l.company_code
LEFT JOIN Senior_Manager s ON c.company_code = s.company_code
LEFT JOIN Manager m ON c.company_code = m.company_code
LEFT JOIN Employee e ON c.company_code = e.company_code
GROUP BY c.company_code, c.founder
굉장히 느리지만 submit 성공..
다른 코드가 있는지 봐야겠다.
'MySQL' 카테고리의 다른 글
[해커랭크] [sql] SQL Project Planning (0) | 2023.02.16 |
---|---|
[해커랭크] [sql] Ollivander's Inventory (0) | 2023.02.13 |
[해커랭크] [SQL] The PADs (1) | 2023.01.30 |
[leetcode] [MySQL] 176. Second Highest Salary (1) | 2022.12.27 |
[oracle] [mysql] group by 시 유의사항 (0) | 2022.12.11 |