두 가지 방법으로 풀었음
1. 서브쿼리를 이용하는 방법
select c.company_code, c.founder, (select count(distinct l.lead_manager_code) from Lead_Manager l where c.company_code=l.company_code), (select count(distinct s.senior_manager_code) from Senior_Manager s where c.company_code=s.company_code), (select count(distinct m.manager_code) from Manager m where c.company_code=m.company_code), (select count(distinct e.employee_code) from Employee e where c.company_code=e.company_code) from Company c group by c.company_code, c.founder order by c.company_code asc; |
2. 모든 테이블을 한 번에 불러와서 쿼리하는 방법
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, Lead_Manager l, Senior_Manager s, Manager m, Employee e where c.company_code = l.company_code and c.company_code = s.company_code and c.company_code = m.company_code and c.company_code = e.company_code group by c.company_code, c.founder order by c.company_code asc; |
'SQL' 카테고리의 다른 글
[PostgreSQL] HA 구성하는 방법 (0) | 2022.07.25 |
---|---|
[Hive] url decoder 예제 (0) | 2022.05.05 |
[MySQL] 여러가지 함수 예제 모음 (2) | 2022.02.13 |
[SQL] WAL 이란 무엇인가? (0) | 2022.02.08 |
[Phoenix] update values 하는 방법 (0) | 2021.11.29 |