MODEL
The MODEL clause is a feature specific to Oracle SQL and is not supported by all DBMS. It is unique to Oracle and provides advanced, spreadsheet-like calculation capabilities within SQL queries, which is not a standard feature across other SQL databases.
Description
The MODEL clause in Oracle SQL is a feature that allows to perform inter-row and inter-column calculations in a spreadsheet-like fashion within a SQL query. It provides a way to define a multidimensional array and apply rules to it, enabling complex data manipulations and calculations.
Key Features of the MODEL Clause
Multidimensional Arrays: Treats query results as multidimensional arrays.
Rules-Based Calculations: Allows the definition of rules for calculations that can span across rows and columns.
Cell References: Supports references to other cells in the array, similar to how spreadsheet cells can reference each other.
Basic Syntax
Here is a simplified syntax of the MODEL clause:
SELECT ...
FROM ...
MODEL
[ PARTITION BY (partition_clause) ]
DIMENSION BY (dimension_clause)
MEASURES (measure_clause)
[ RULES (rule_clause) ];PARTITION BY: Divides the data into partitions, similar to a
GROUP BYclause.DIMENSION BY: Specifies the dimensions of the array (e.g., rows or columns). In the context of the
MODELclause, dimensions act like rows in a spreadsheetMEASURES: Defines the measures or the actual data values that will be manipulated. Measures act like the data cells in a spreadsheet that can be manipulated.
RULES: Contains the rules that define how calculations are performed.
Example
Sample Data
Simple example with no calculations or modifications (rules) applied to the data.

Addressing a specific cell

Addressing a range of cells (using a loop)

Use of pseudo function cv()

Addressing a range of cells (using a list of values)

Addressing a range of cells (using a subquery)

The pseudo keyword ANY

Addressing cells relative to the current cell

Generating data

Row returning behaviour

Last updated