๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Coding Test/SQL

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋Œ€์žฅ๊ท ์˜ ํฌ๊ธฐ์— ๋”ฐ๋ผ ๋ถ„๋ฅ˜ํ•˜๊ธฐ2/MySQL - Lv.3

by The Future Engineer, Lucy 2024. 10. 31.
728x90
๋ฐ˜์‘ํ˜•

โ“๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/301649

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก, ์ฑ„์šฉ๊นŒ์ง€ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr

๋ฌธ์ œ ์„ค๋ช…

๋Œ€์žฅ๊ท ๋“ค์€ ์ผ์ • ์ฃผ๊ธฐ๋กœ ๋ถ„ํ™”ํ•˜๋ฉฐ, ๋ถ„ํ™”๋ฅผ ์‹œ์ž‘ํ•œ ๊ฐœ์ฒด๋ฅผ ๋ถ€๋ชจ ๊ฐœ์ฒด, ๋ถ„ํ™”๊ฐ€ ๋˜์–ด ๋‚˜์˜จ ๊ฐœ์ฒด๋ฅผ ์ž์‹ ๊ฐœ์ฒด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
๋‹ค์Œ์€ ์‹คํ—˜์‹ค์—์„œ ๋ฐฐ์–‘ํ•œ ๋Œ€์žฅ๊ท ๋“ค์˜ ์ •๋ณด๋ฅผ ๋‹ด์€ ECOLI_DATA ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค. ECOLI_DATA ํ…Œ์ด๋ธ”์˜ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ, ID, PARENT_ID, SIZE_OF_COLONY, DIFFERENTIATION_DATE, GENOTYPE ์€ ๊ฐ๊ฐ ๋Œ€์žฅ๊ท  ๊ฐœ์ฒด์˜ ID, ๋ถ€๋ชจ ๊ฐœ์ฒด์˜ ID, ๊ฐœ์ฒด์˜ ํฌ๊ธฐ, ๋ถ„ํ™”๋˜์–ด ๋‚˜์˜จ ๋‚ ์งœ, ๊ฐœ์ฒด์˜ ํ˜•์งˆ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

Column name Type Nullable
ID INTEGER FALSE
PARENT_ID INTEGER TRUE
SIZE_OF_COLONY INTEGER FALSE
DIFFERENTIATION_DATE DATE FALSE
GENOTYPE INTEGER FALSE

์ตœ์ดˆ์˜ ๋Œ€์žฅ๊ท  ๊ฐœ์ฒด์˜ PARENT_ID ๋Š” NULL ๊ฐ’์ž…๋‹ˆ๋‹ค.


๋ฌธ์ œ

๋Œ€์žฅ๊ท  ๊ฐœ์ฒด์˜ ํฌ๊ธฐ๋ฅผ ๋‚ด๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ–ˆ์„ ๋•Œ ์ƒ์œ„ 0% ~ 25% ๋ฅผ 'CRITICAL', 26% ~ 50% ๋ฅผ 'HIGH', 51% ~ 75% ๋ฅผ 'MEDIUM', 76% ~ 100% ๋ฅผ 'LOW' ๋ผ๊ณ  ๋ถ„๋ฅ˜ํ•ฉ๋‹ˆ๋‹ค. ๋Œ€์žฅ๊ท  ๊ฐœ์ฒด์˜ ID(ID) ์™€ ๋ถ„๋ฅ˜๋œ ์ด๋ฆ„(COLONY_NAME)์„ ์ถœ๋ ฅํ•˜๋Š” SQL ๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ์ด๋•Œ ๊ฒฐ๊ณผ๋Š” ๊ฐœ์ฒด์˜ ID ์— ๋Œ€ํ•ด ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ์„ธ์š” . ๋‹จ, ์ด ๋ฐ์ดํ„ฐ์˜ ์ˆ˜๋Š” 4์˜ ๋ฐฐ์ˆ˜์ด๋ฉฐ ๊ฐ™์€ ์‚ฌ์ด์ฆˆ์˜ ๋Œ€์žฅ๊ท  ๊ฐœ์ฒด๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ๋ถ„๋ฅ˜๋˜๋Š” ๊ฒฝ์šฐ๋Š” ์—†์Šต๋‹ˆ๋‹ค.


์˜ˆ์‹œ

์˜ˆ๋ฅผ ๋“ค์–ด ECOLI_DATA ํ…Œ์ด๋ธ”์ด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค๋ฉด

 ID PARENT_ID SIZE_OF_COLONY DIFFERENTIATION_DATE GENOTYPE
1 NULL 10 2019/01/01 5
2 NULL 2 2019/01/01 3
3 1 100 2020/01/01 4
4 2 16 2020/01/01 4
5 2 17 2020/01/01 6
6 4 101 2021/01/01 22
7 6 101 2022/01/01 23
8 6 1 2022/01/01 27

๊ธฐ์ค€์— ์˜ํ•ด ๋ถ„๋ฅ˜๋œ ๋Œ€์žฅ๊ท ๋“ค์˜ ID๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

CRITICAL (์ƒ์œ„ 0% ~ 25%) : ID 6, ID 7
HIGH (์ƒ์œ„ 26% ~ 50%) : ID 3, ID 5
MEDIUM (์ƒ์œ„ 51% ~ 75%) : ID 1, ID 4
LOW (์ƒ์œ„ 76% ~ 100%) : ID 2, ID 8

๋”ฐ๋ผ์„œ ๊ฒฐ๊ณผ๋ฅผ ID ์— ๋Œ€ํ•ด ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.

ID COLONY_NAME
1 MEDIUM
2 LOW
3 HIGH
4 MEDIUM
5 HIGH
6 CRITICAL
7 CRITICAL
8 LOW

โœ๐Ÿปํ’€์ด

percent_rank()๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒ๋Œ€ ์ˆœ์œ„๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.
๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋Œ€์žฅ๊ท  ๊ฐœ์ฒด์˜ ํฌ๊ธฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ–ˆ์„ ๋•Œ์˜ ์ƒ์œ„ ํผ์„ผํŠธ๋ฅผ ๊ตฌํ•œ๋‹ค.
id์™€ percent_rank๋งŒ ์žˆ๋Š” ํ…Œ์ด๋ธ”์„ ๊ตฌํ•œ๋‹ค.
๊ตฌํ•œ ํ…Œ์ด๋ธ”๋กœ๋ถ€ํ„ฐ ๊ฐ ๊ฒฝ์šฐ๋ฅผ ๋‚˜๋ˆ  ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค. 

๐Ÿ’ป์ฝ”๋“œ

select id, case
                when size_rank <= 0.25 then 'CRITICAL'
                when size_rank <= 0.50 then 'HIGH'
                when size_rank <= 0.75 then 'MEDIUM'
                else 'LOW'
            end as colony_name
from (select id, percent_rank() over (order by size_of_colony desc) as size_rank
     from ecoli_data) ed
order by id

๐Ÿ’ก์ƒˆ๋กœ ๋ฐฐ์šด ๋‚ด์šฉ

์ƒ๋Œ€ ์ˆœ์œ„๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜ - percent_rank

  • percent_rankํ•จ์ˆ˜์˜ ๊ธฐ๋ณธ ํ˜•์‹
    percent_rank() over ([partition by ์—ด] order by ์—ด)
  • percent_rank๋Š” ์ง€์ •ํ•œ ๊ทธ๋ฃน ๋˜๋Š” ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋กœ ์ด๋ฃจ์–ด์ง„ ๊ทธ๋ฃน ๋‚ด์˜ ์ƒ๋Œ€ ์ˆœ์œ„๋ฅผ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋‹ค.
  • percent_rankํ•จ์ˆ˜๋Š” cume_dist ํ•จ์ˆ˜์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ ๋ˆ„์  ๋ถ„ํฌ๊ฐ€ ์•„๋‹Œ ๋ถ„ํฌ ์ˆœ์œ„์ด๋‹ค.

๋ˆ„์  ๋ถ„ํฌ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜ - cume_dist

  • cume_distํ•จ์ˆ˜์˜ ๊ธฐ๋ณธ ํ˜•์‹
    cume_dist() over ([partition by ์—ด] order by ์—ด)
  • cume_distํ•จ์ˆ˜๋Š” 0์ดˆ๊ณผ 1์ดํ•˜ ๋ฒ”์œ„ ๊ฐ’์„ ๋ฐ˜ํ™˜, ๊ฐ™์€ ๊ฐ’์€ ํ•ญ์ƒ ๊ฐ™์€ ๋ˆ„์  ๋ถ„ํฌ๊ฐ’์œผ๋กœ ๊ณ„์‚ฐ.
  • cume_dist๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ null๊ฐ’์„ ํฌํ•จํ•˜๋ฉฐ null๊ฐ’์€ ํ•ด๋‹น ๋ฐ์ดํ„ฐ ์ง‘ํ•ฉ์—์„œ ๊ฐ€์žฅ ๋‚ฎ์€ ๊ฐ’์œผ๋กœ ์ทจ๊ธ‰.

๐Ÿ“ํ›„๊ธฐ

์ƒ์œ„ ํผ์„ผํŠธ๋ฅผ ๋ณด์ž๋งˆ์ž percent_rank๊ฐ€ ๋– ์˜ฌ๋ž์ง€๋งŒ ์–ด๋–ป๊ฒŒ ์“ฐ๋Š”์ง€๋Š” ๋ชฐ๋ž๋‹ค.
์ง€๊ธˆ๊นŒ์ง€ ์ง๋ฌดํ…Œ์ŠคํŠธ ๋ณด๋ฉด์„œ ์ด ์ •๋„์˜ ํ•จ์ˆ˜๋Š” ์“ด ์ ์ด ์—†์–ด์„œ ๋‚˜์ค‘์— ํ•„์š”ํ•  ๋•Œ ์ฐพ์•„๋ณด๋ฉด ๋˜์ง€ ํ–ˆ๋Š”๋ฐ ํ˜น์‹œ ๋ชจ๋ฅด๋‹ˆ ์ด๋Ÿฐ ํ•จ์ˆ˜๋„ ๋งŽ์ด ์•Œ์•„๋‘ฌ์•ผ๊ฒ ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•