๐Ÿ’ป

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต > SELECT > ๋Œ€์žฅ๊ท ์˜ ํฌ๊ธฐ์— ๋”ฐ๋ผ ๋ถ„๋ฅ˜ํ•˜๊ธฐ 2 ๋ณธ๋ฌธ

์•Œ๊ณ ๋ฆฌ์ฆ˜/๊ธฐ์ดˆ๋‹ค์ง€๊ธฐ

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต > SELECT > ๋Œ€์žฅ๊ท ์˜ ํฌ๊ธฐ์— ๋”ฐ๋ผ ๋ถ„๋ฅ˜ํ•˜๊ธฐ 2

๋˜ํšจ๋‹ˆ 2024. 11. 2. 01:06

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

 

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

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

programmers.co.kr

LV3. ๋Œ€์žฅ๊ท ์˜ ํฌ๊ธฐ์— ๋”ฐ๋ผ ๋ถ„๋ฅ˜ํ•˜๊ธฐ 2

 

 

๐Ÿ’ก์•„์ด๋””์–ด

percent_rank() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ’€๋ฆฌ๋Š” ๋ฌธ์ œ์ด๋‹ค.

case-when ๋˜๋Š” if ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์กฐ๊ฑด๋ฌธ์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ณ , Join์„ ์‚ฌ์šฉํ•œ๋‹ค.

 

 

๐Ÿ’ป ์ฝ”๋“œ์ž‘์„ฑ

-- ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”
-- 1. IF๋ฌธ ์‚ฌ์šฉ
# SELECT A.ID
#         , IF(B.RK <= 0.25, 'CRITICAL', 
#              IF(B.RK <= 0.50, 'HIGH', 
#                 IF(B.RK <= 0.75, 'MEDIUM', 'LOW'))) 
#                     as COLONY_NAME
# FROM ECOLI_DATA A, 
#         (SELECT ID
#                ,PERCENT_RANK() OVER (ORDER BY SIZE_OF_COLONY DESC) AS RK
#         FROM ECOLI_DATA) B
# WHERE A.ID = B.ID;

-- 2. CASE๋ฌธ ์‚ฌ์šฉ
SELECT A.ID
    , CASE WHEN B.RK <= 0.25 THEN 'CRITICAL'
           WHEN B.RK <= 0.50 THEN 'HIGH'
           WHEN B.RK <= 0.75 THEN 'MEDIUM'
           ELSE 'LOW'
      END AS COLONY_NAME
FROM ECOLI_DATA A
JOIN
(SELECT ID
    , PERCENT_RANK() OVER (ORDER BY SIZE_OF_COLONY DESC) AS RK
FROM ECOLI_DATA) B
ON A.ID = B.ID
๋ฐ˜์‘ํ˜•
Comments