Search

New Companies

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 JOIN Lead_Manager l ON c.company_code = l.company_code JOIN Senior_Manager s ON l.lead_manager_code = s.lead_manager_code JOIN Manager m ON s.senior_manager_code = m.senior_manager_code JOIN Employee e ON m.manager_code = e.manager_code GROUP BY 1, 2 ORDER BY c.company_code ;
SQL
๋ณต์‚ฌ
์ค‘๋ณต ์—†์ด ์„ธ๊ธฐ ์œ„ํ•ด์„œ๋Š” count(distinct(COL))
C1 Angela 1 2 5 13 C10 Earl 1 1 2 3 C100 Aaron 1 2 4 10 C11 Robert 1 1 1 1 C12 Amy 1 2 6 14
SQL
๋ณต์‚ฌ

๋ชจ๋ฒ” ๋‹ต์•ˆ

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 l.lead_manager_code = s.lead_manager_code LEFT JOIN Manager m ON s.senior_manager_code = m.senior_manager_code LEFT JOIN Employee e ON m.manager_code = e.manager_code GROUP BY 1, 2 ORDER BY c.company_code ;
SQL
๋ณต์‚ฌ
์ค‘๋ณต ์—†์ด ์„ธ๊ธฐ ์œ„ํ•ด์„œ๋Š” count(distinct(COL))
count๋Š” null์„ ์„ธ์ง€ ์•Š๋Š”๋‹ค.
๊ทธ๋ฆฌ๊ณ  LEFT JOIN์„ ํ†ตํ•ด ์™ผ์ชฝ ๋Œ€์ƒ ์ค‘์‹ฌ์œผ๋กœ join ํ•œ๋‹ค
๊ฐ ์กฐ์ธ๋˜๋Š” ์™ผ์ชฝ ๋Œ€์ƒ ์ค‘์‹ฌ์œผ๋กœ ์›์†Œ๊ฐ€ ์ฐํž ๊ฒƒ์ด๋‹ค
1 2
1 NULL
2 3
c.company_code๋ฅผ ๊ธฐ์ค€์œผ๋กœ count distinct๋ฅผ ํ•˜๊ฒŒ ๋˜๋ฉด
๊ฐ ์กฐ์ธ ์‹œ์ ๋˜๋Š” ์‹œ์ ์—์„œ์˜ ๊ณ ์œ  ๊ฐœ์ˆ˜๋ฅผ ์„ธ์ฃผ๊ฒŒ ๋œ๋‹ค