๐ป
[ํ๋ก๊ทธ๋๋จธ์ค] ์ฝ๋ฉํ ์คํธ ์ฐ์ต > SELECT > ๋์ฅ๊ท ์ ํฌ๊ธฐ์ ๋ฐ๋ผ ๋ถ๋ฅํ๊ธฐ 2 ๋ณธ๋ฌธ
์๊ณ ๋ฆฌ์ฆ/๊ธฐ์ด๋ค์ง๊ธฐ
[ํ๋ก๊ทธ๋๋จธ์ค] ์ฝ๋ฉํ ์คํธ ์ฐ์ต > SELECT > ๋์ฅ๊ท ์ ํฌ๊ธฐ์ ๋ฐ๋ผ ๋ถ๋ฅํ๊ธฐ 2
๋ํจ๋ 2024. 11. 2. 01:06https://school.programmers.co.kr/learn/courses/30/lessons/301649#qna
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