Oracle analytic window functions
Oracle's analytic functions are window functions by another name, with ROWS/RANGE/GROUPS frames, IGNORE NULLS and Oracle-only extras such as KEEP and RATIO_TO_REPORT.
- 카테고리
- 창
- Returns
per-partition value- In this category
- 75 of 79
구문
SUM(x) OVER (PARTITION BY g ORDER BY d RANGE BETWEEN INTERVAL '1' DAY PRECEDING AND CURRENT ROW)
엔진 지원
| 엔진 | Support | 메모 |
|---|---|---|
| SQL Server | Not built in | No built-in equivalent. |
| PostgreSQL | Not built in | No built-in equivalent. |
| MySQL | Not built in | No built-in equivalent. |
| SQLite | Not built in | No built-in equivalent. |
| Oracle | 예 | analytic clauses on most aggregates; keyword orders differ from other engines (x IGNORE NULLS, LISTAGG ... WITHIN GROUP) |
예시
Oracle
SELECT department, employee_name, salary,
SUM(salary) OVER (PARTITION BY department ORDER BY hire_date) AS running_payroll,
RATIO_TO_REPORT(salary) OVER (PARTITION BY department) AS salary_share
FROM employees;
알아 두면 좋은 정보
- Oracle treats NULL as larger than any value: last ascending, first descending. PostgreSQL does the same; SQL Server, MySQL and SQLite do the opposite.
- Oracle accepts IGNORE NULLS inside the parentheses, FIRST_VALUE(x IGNORE NULLS), as well as after them; LISTAGG takes WITHIN GROUP (ORDER BY ...) before an OVER that may hold only PARTITION BY.
- RANGE frames accept INTERVAL literals and NUMTODSINTERVAL for calendar windows. GROUPS, EXCLUDE and the WINDOW clause need 21c or later, so 19c code cannot use them.
Questions about Oracle Analytic Functions
What is the difference between aggregate and analytic in Oracle?
Only whether OVER is present: SUM(x) collapses rows, SUM(x) OVER (...) keeps them. Oracle offers analytic forms for functions the others do not, such as COUNT(DISTINCT x), PERCENTILE_CONT and the KEEP aggregates.
Why do my Oracle windows sort NULLs differently?
Oracle sorts NULLs last when ascending and first when descending, treating NULL as larger than everything. SQL Server, MySQL and SQLite treat it as smaller, so the same query puts NULLs at the other end. Spell NULLS FIRST/LAST.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.