μλμ° ν¨μ
νκ³Ό ν κ°μ κ΄κ³λ₯Ό μ μνλ ν¨μ
μμ, ν©κ³, νκ· ν μμΉ λ± κ³μ° κ²°κ³Όλ₯Ό μ¬μ©ν μ μλ€.
μ’ λ₯
β’
μμ ν¨μ
β’
μ§κ³ ν¨μ
β’
ν μμ ν¨μ
β’
λΉμ¨ ν¨μ
λ¬Έλ² κ΅¬μ‘°
SELECT μλμ°ν¨μ( μΈμ )
OVER (
[PARTITION BY 컬λΌ1, 컬λΌ2, ...]
[ORDER BY 컬λΌ1 [ASC | DESC], 컬λΌ2 [ASC | DESC], ...]
[WINDOWING μ ]
)
FROM ν
μ΄λΈ;
SQL
볡μ¬
β’
κΈ°λ³Έ κ΅¬μ± μμ
κ΅¬μ± μμ | μ€λͺ
|
ν¨μ | μλμ° ν¨μμ μ’
λ₯λ₯Ό λνλ
λλ€. μλ₯Ό λ€μ΄, SUM(), AVG(), ROW_NUMBER() λ±μ΄ μμ΅λλ€. |
OVER | μλμ° ν¨μλ₯Ό μ¬μ©νκΈ° μν ν€μλμ
λλ€. |
PARTITION BY | μ νμ μΌλ‘ μ¬μ©λ©λλ€.
λ°μ΄ν°λ₯Ό νν°μ
μΌλ‘ λλλ λ° μ¬μ©λ©λλ€. |
ORDER BY | μ νμ μΌλ‘ μ¬μ©λ©λλ€. μλμ° ν¨μλ₯Ό μ μ©ν λ μ λ ¬ μμλ₯Ό μ§μ ν©λλ€. κΈ°λ³Έμ μΌλ‘ μ λ ¬ μμλ νμ¬ νμ μ€μ¬μΌλ‘ μ μ©λ©λλ€. |
β’
WINDOWING μ
μ©μ΄ | μ€λͺ
|
ROWS | ROWS ν€μλλ μλμ°μμ ROWSλ₯Ό κΈ°μ€μΌλ‘ λ²μλ₯Ό μ§μ ν©λλ€. μλ₯Ό λ€μ΄, ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWINGμ νμ¬ νμ μ€μ¬μΌλ‘ μμ 1κ°μ νκ³Ό λ€μ 1κ°μ νμ ν¬ν¨νλ μλμ° λ²μλ₯Ό μ μν©λλ€. |
RANGE | RANGE ν€μλλ μλμ°μμ κ°μ λ²μλ₯Ό κΈ°μ€μΌλ‘ λ²μλ₯Ό μ§μ ν©λλ€. μ£Όλ‘ μ«μλ λ μ§μ κ°μ μμν λ°μ΄ν° μ νμ 컬λΌμ λν΄ μ¬μ©λ©λλ€. |
BETWEEN~AND | BETWEEN~AND ν€μλλ μλμ°μ λ²μλ₯Ό μ§μ ν λ μ¬μ©λ©λλ€. μλ₯Ό λ€μ΄, ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWINGκ³Ό κ°μ΄ μ¬μ©λ©λλ€. |
UNBOUNDED PRECEDING | UNBOUNDED PRECEDINGμ μλμ°μ μμμ λνλ΄λ©°, νμ¬ νλ³΄λ€ μμ λͺ¨λ νμ ν¬ν¨ν©λλ€. |
UNBOUNDED FOLLOWING | UNBOUNDED FOLLOWINGμ μλμ°μ λμ λνλ΄λ©°, νμ¬ νλ³΄λ€ λ€μ λͺ¨λ νμ ν¬ν¨ν©λλ€. |
CURRENT ROW | CURRENT ROWλ μλμ°μμ νμ¬ νμ λνλ
λλ€. |
PRECEDING | νμ¬ ν μ΄μ μ μμΉν νλ€ κ°λ¦¬ν΅λλ€. |
FOLLOWING | νμ¬ ν μ΄νμ μμΉν νλ€μ κ°λ¦¬ν΅λλ€. |
μμ ν¨μ
κ²°κ³Ό μ§ν© λ΄μμ νμ μμλ₯Ό κ²°μ νλ ν¨μ
ν¨μλͺ
| μ€λͺ
|
RANK | λμΌν κ°μ λν μμλ₯Ό λΆμ¬νκ³ κ°μ μμμ λμΌν κ°μ΄ μλ€λ©΄ λ€μ μμλ 건λλ°κ³ κ·Έ λ€μ μμλ₯Ό λΆμ¬νλ€. |
DENSE_RANK | λμΌν κ°μ λν μμλ₯Ό λΆμ¬νκ³ κ°μ μμμ λμΌν κ°μ΄ μλ€λ©΄ λ€μ μμλ κ·Έ λ€μ μμμ λμΌν κ°μ κ°μ§λ€. |
ROW_NUMBER | μμλλ‘ μ«μλ₯Ό λΆμ¬νμ¬ κ° νμ λν μ μΌν μμλ₯Ό λΆμ¬νλ€. |
μμ ν¨μ μμ μ½λ
β’
RANK
β’
DENSE_RANK
β’
ROW_NUMBER
RANK
β’
κΈμ¬ λ΄λ¦Όμ°¨μμΌλ‘ μμλ₯Ό ꡬνμμ€. (곡λ μμλ©΄ λ€μ μμ μμ°)
SELECT employee_id, salary
, RANK()
OVER (ORDER BY salary DESC) AS salary_rank
FROM employees;
SQL
볡μ¬
DENSE_RANK
β’
κΈμ¬ λ΄λ¦Όμ°¨μμΌλ‘ μμλ₯Ό ꡬνμμ€. (곡λ μμκ° μμ΄λ λ€μ μμ μ΄λ¦Ό)
SELECT employee_id
,salary
,DENSE_RANK()
OVER (ORDER BY salary DESC) AS dense_salary_rank
FROM employees;
SQL
볡μ¬
ROW_NUMBER
β’
μ€λ³΅ μμ μκ΄μμ΄, μ μΌν μμ λ²νΈ λ§ κ·Έλλ‘ νλ²νΈλ₯Ό μΆλ ₯
SELECT employee_id
,salary
,ROW_NUMBER()
OVER (ORDER BY salary DESC) AS row_num
FROM employees;
SQL
볡μ¬
μ§κ³ ν¨μ
μ¬λ¬ νμ κ°μ κ·Έλ£Ήννκ³ μ§κ³νμ¬ λ¨μΌ κ²°κ³Ό κ°μ λ°ννλ ν¨μ
ν¨μλͺ
| μ€λͺ
|
SUM | μ«μν λ°μ΄ν°μ ν©μ κ³μ°ν©λλ€. |
AVG | μ«μν λ°μ΄ν°μ νκ· μ κ³μ°ν©λλ€. |
COUNT | κ²°κ³Ό μ§ν©μ΄λ κ·Έλ£Ήμ ν μλ₯Ό κ³μ°ν©λλ€. |
MAX | μ«μλ λ μ§ λ°μ΄ν°μ μ΅λκ°μ μ°Ύμ΅λλ€. |
MIN | μ«μλ λ μ§ λ°μ΄ν°μ μ΅μκ°μ μ°Ύμ΅λλ€. |
μ§κ³ ν¨μ μμ μ½λ
β’
SUM
β’
AVG
β’
COUNT
β’
MAX
β’
MIN
SUM
SELECT SUM(salary) AS total_salary
FROM employees;
SQL
볡μ¬
AVG
SELECT AVG(salary) AS average_salary
FROM employees;
SQL
볡μ¬
COUNT
SELECT COUNT(*) AS total_employees
FROM employees;
SQL
볡μ¬
MAX
SELECT MAX(salary) AS max_salary
FROM employees;
SQL
볡μ¬
MIN
SELECT MIN(salary) AS min_salary
FROM employees;
SQL
볡μ¬
ν μμ ν¨μ
κ²°κ³Ό μ§ν© λ΄μμ νΉμ νμ μμΉλ μμλ₯Ό κ²°μ νλ ν¨μ
ν¨μλͺ
| μ€λͺ
|
FIRST_VALUE | κ·Έλ£Ή λ΄μμ 첫 λ²μ§Έ νμ κ°μ λ°νν©λλ€. |
LAST_VALUE | κ·Έλ£Ή λ΄μμ λ§μ§λ§ νμ κ°μ λ°νν©λλ€. |
LAG | νμ¬ ν μ΄μ μ κ°μ κ°μ Έμ΅λλ€. |
LEAD | νμ¬ ν λ€μμ κ°μ κ°μ Έμ΅λλ€. |
ν μμ ν¨μ μμ μ½λ
β’
FIRST_VALUE
β’
LAST_VALUE
β’
LAG
β’
LEAD
FIRST_VALUE
β’
κ° λΆμλ³λ‘ 첫 λ²μ§Έλ‘ μ
μ¬ν μ§μμ κΈμ¬λ₯Ό μ‘°ν
SELECT department_id
, employee_id
, salary
,FIRST_VALUE(salary)
OVER (PARTITION BY department_id
ORDER BY hire_date) AS first_salary
FROM employees;
SQL
볡μ¬
LAST_VALUE
β’
κ° λΆμλ³λ‘ λ§μ§λ§μΌλ‘ μ
μ¬ν μ§μμ κΈμ¬λ₯Ό μ‘°ν
SELECT department_id
, employee_id
, salary
, LAST_VALUE(salary)
OVER (PARTITION BY department_id
ORDER BY hire_date
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS last_salary
FROM employees;
SQL
볡μ¬
LAG
β’
μ΄μ μ§μμ μ 보λ₯Ό μ‘°ν
SELECT employee_id, first_name, hire_date,
LAG(first_name) OVER (ORDER BY hire_date) AS previous_name,
LAG(hire_date) OVER (ORDER BY hire_date) AS previous_hire_date
FROM employees;
SQL
볡μ¬
LEAD
β’
λ€μ μ§μμ μ 보λ₯Ό μ‘°ν
SELECT employee_id, first_name, hire_date,
LEAD(first_name) OVER (ORDER BY hire_date) AS next_first_name,
LEAD(hire_date) OVER (ORDER BY hire_date) AS next_hire_date
FROM employees;
SQL
볡μ¬
λΉμ¨ ν¨μ
κ²°κ³Ό μ§ν© λ΄μμ νΉμ κ°μ λΉμ¨μ΄λ λ°±λΆμ¨μ κ³μ°νλ ν¨μ
ν¨μλͺ
| μ€λͺ
|
CUME_DIST | κ²°κ³Ό μ§ν©μμ νμ¬ νμ λμ λΆν¬ κ°μ κ³μ°ν©λλ€. |
PERCENT_RANK | κ²°κ³Ό μ§ν©μμ νμ¬ νμ λ°±λΆμ μμλ₯Ό κ³μ°ν©λλ€. |
NTILE | κ²°κ³Ό μ§ν©μ μ§μ λ κ°μμ λμΌν ν¬κΈ°μ κ·Έλ£ΉμΌλ‘ λλκ³ , κ° νμ κ·Έλ£Ή λ²νΈλ₯Ό ν λΉν©λλ€. |
RATIO_TO_REPORT | κ²°κ³Ό μ§ν©μ κ° κ·Έλ£Ήμμ νμ¬ νμ λΉμ¨μ κ³μ°ν©λλ€. |
λΉμ¨ ν¨μ μμ μ½λ
β’
CUME_DIST
β’
PERCENT_RANK
β’
NTILE
β’
RATIO_TO_REPORT
CUME_DIST
β’
μ΄ ν¨μλ κ²°κ³Ό μ§ν©μμ νμ¬ νμ λμ λΆν¬ κ°μ κ³μ°
SELECT employee_id
, salary
, CUME_DIST()
OVER (ORDER BY salary DESC) AS cumulative_distribution
FROM employees;
SQL
볡μ¬
PERCENT_RANK
β’
μ΄ ν¨μλ κ²°κ³Ό μ§ν©μμ νμ¬ νμ λ°±λΆμ μμλ₯Ό κ³μ°
SELECT employee_id
, salary
, PERCENT_RANK()
OVER (ORDER BY salary DESC) AS percent_rank
FROM employees;
SQL
볡μ¬
NTILE
β’
μ΄ ν¨μλ κ²°κ³Ό μ§ν©μ μ§μ λ κ°μμ λμΌν ν¬κΈ°μ κ·Έλ£ΉμΌλ‘ λλκ³ , κ° νμ κ·Έλ£Ή λ²νΈλ₯Ό ν λΉ
SELECT employee_id
, salary
, NTILE(4)
OVER (ORDER BY salary DESC) AS quartile
FROM employees;
SQL
볡μ¬
RATIO_TO_REPORT
β’
μ΄ ν¨μλ κ²°κ³Ό μ§ν©μ κ° κ·Έλ£Ήμμ νμ¬ νμ λΉμ¨μ κ³μ°
SELECT department_id
, employee_id
, salary
, RATIO_TO_REPORT(salary)
OVER (PARTITION BY department_id) AS salary_ratio
FROM employees;
SQL
볡μ¬