λ¨Όμ μμκ°κΈ°!
- μ§μ μ΄λ‘μμΌλ‘ μμ± λ λΆλΆμ μλ΅ κ°λ₯ν λΆλΆμ λλ€.
- νΉμ μμ± λ³λ‘ ꡬλΆνκ³ μ ν λ(=GROUP BY μ κ°μ λμμ μν€κ³ μΆμ λ) PARTITION BYλ₯Ό μ¬μ©ν©λλ€.
- κΈ°λ³Έ μμ μΈμ μΆκ° μμλ₯Ό λ³΄λ €λ©΄ λ보기λ₯Ό ν΄λ¦νμΈμ π!
RANK
μ€λ³΅ κ°λ€μ λν΄μ λμΌ μμλ‘ νμνκ³ , μ€λ³΅ μμ λ€μ κ°μ λν΄μλ μ€λ³΅ κ°μλ§νΌ λ¨μ΄μ§ μμλ‘ μΆλ ₯νλ ν¨μ
rank(κ°) over (partition by λμ order by λμ)
select EMPNO,
ENAME,
SAL,
rank() over (order by SAL desc) "RANK"
from EMP;
μμ 1)
-- ENAME λ±μ μΆλ ₯νκΈ° μν 쿼리
/*select ename, rank() over (order by ENAME) "ENAME RANK"
from EMP;*/
-- ENAME λ±μ μ€ 'FORD'μ λ±μλ₯Ό μΆλ ₯νκΈ° μν 쿼리
select rank('FORD') within group (order by ENAME) "RANK"
from EMP;
μμ 2) EMP ν μ΄λΈμ μ‘°ννμ¬ EMPNO, ENAME, SAL, λΆμλ²νΈ(DEPTNO) λ³ κΈμ¬(SAL) λ±μ μΆλ ₯
select EMPNO, ENAME, SAL, DEPTNO, rank() over (partition by DEPTNO order by SAL desc) "RANK"
from EMP
order by DEPTNO, RANK;
μμ 3) EMP ν μ΄λΈμ μ‘°ννμ¬ EMPNO, ENAME, SAL, DEPTNO, κ°μ λΆμ λ΄ JOBλ³ κΈμ¬ μμλ₯Ό μΆλ ₯
select EMPNO, ENAME, SAL, DEPTNO, JOB, rank() over (partition by DEPTNO, JOB order by SAL desc) "RANK"
from EMP;
DENSE_RANK
μ€λ³΅ κ°λ€μ λν΄μλ λμΌ μμλ‘ νμνκ³ , μ€λ³΅ μμ λ€μ κ°μ λν΄μλ μ€λ³΅ κ° κ°μμ μκ΄μμ΄ μμ°¨μ μΈ μμ κ°μ μΆλ ₯νλλ‘ νλ ν¨μ
dense_rank(κ°) over (partition by λμ order by λμ)
select EMPNO,
ENAME,
SAL,
dense_rank() over (order by SAL desc) "RANK"
from EMP;
ROW_NUMBER
μ€λ³΅ κ°λ€μ λν΄μλ μμ°¨μ μΈ μμλ₯Ό νμνλλ‘ μΆλ ₯νλ ν¨μ
row_number(κ°) over (partition by λμ order by λμ)
select EMPNO,
ENAME,
SAL,
row_number() over (order by SAL desc) "RANK"
from EMP;
Tip. RANK, DENSE_RANK, ROW_NUMBER ν¨μ λΉκ΅μ 리!
select ENAME,
SAL,
rank() over (order by SAL desc) "RANK",
dense_rank() over (order by SAL desc) "DENSE_RANK",
row_number() over (order by SAL desc) "ROW_NUMBER"
from EMP;
ν¨μ | μ€λ³΅ κ° μμ | λ€μ μμ |
RANK | λμΌ μμ | +μ€λ³΅ κ° κ°μ |
DENSE_RANK | λμΌ μμ | +1 |
ROW_NUMBER | μμ°¨μ μμ |
NTILE
μ§μ ν μ«μ λ§νΌ λ±μλ₯Ό λ±λΆνμ¬ μΆλ ₯νλ ν¨μ
ntile(μ«μ) orver(partition by λμ orver by λμ)
select EMPNO,
ENAME,
SAL,
ntile(4) over (order by SAL desc) "RANK"
from EMP;
Thanks for π
'Database' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
β solved * MySQL: ERROR 1418 (HY000): ... (0) | 2021.06.13 |
---|---|
SQL λμ ν©κ³ ν¨μ: SUM OVER (0) | 2021.03.23 |
SQL Oracle * LAG/LEAD: μ΄μ /μ΄ν λ‘μ° κ° λ°ν (0) | 2021.03.23 |
SQL 볡μν ν¨μ: GROUP ν¨μμ μ’ λ₯ (0) | 2021.03.16 |
β Solved * oracle sqldeveloper μ°κ²° μ μλ¬? ( at. Mac ) (28) | 2021.03.09 |