Search This Blog

Thursday 2 May 2013

Oracle Analytical Function PERCENT_RANK With Example

PERCENT_RANK
------------

PERCENT_RANK is similar to the CUME_DIST (cumulative distribution) function. The range of values returned by PERCENT_RANK is 0 to 1, inclusive. The first row in any set has a PERCENT_RANK of 0. The return value is NUMBER.

  • As an aggregate function, PERCENT_RANK calculates, for a hypothetical row r identified by the arguments of the function and a corresponding sort specification, the rank of row r minus 1 divided by the number of rows in the aggregate group. This calculation is made as if the hypothetical row r were inserted into the group of rows over which Oracle Database is to aggregate.
    The arguments of the function identify a single hypothetical row within each aggregate group. Therefore, they must all evaluate to constant expressions within each aggregate group. The constant argument expressions and the expressions in the ORDER BY clause of the aggregate match by position. Therefore the number of arguments must be the same and their types must be compatible.
  • As an analytic function, for a row r, PERCENT_RANK calculates the rank of r minus 1, divided by 1 less than the number of rows being evaluated (the entire query result set or a partition).

  • The following example calculates the percent rank of a hypothetical employee in the sample table hr.employees with a salary of $15,500 and a commission of 5%:
    SELECT PERCENT_RANK(15000, .05) WITHIN GROUP
           (ORDER BY salary, commission_pct) "Percent-Rank" 
      FROM employees;
    
    Percent-Rank
    ------------
      .971962617
    
    Analytic Example
    The following example calculates, for each employee, the percent rank of the employee's salary within the department:
    SELECT department_id, last_name, salary, PERCENT_RANK() 
           OVER (PARTITION BY department_id ORDER BY salary DESC) AS pr
      FROM employees
      ORDER BY pr, salary, last_name;
    
    DEPARTMENT_ID LAST_NAME                     SALARY         PR
    ------------- ------------------------- ---------- ----------
               10 Whalen                          4400          0
               40 Mavris                          6500          0
                  Grant                           7000          0
    . . .
               80 Vishney                        10500 .181818182
               80 Zlotkey                        10500 .181818182
               30 Khoo                            3100         .2
    . . .
               50 Markle                          2200 .954545455
               50 Philtanker                      2200 .954545455
               50 Olson                           2100          1
    . . .
    

    Oracle Analytical Function NTILE With Example

    NTILE
    ---------

    NTILE is an analytic function. It divides an ordered data set into a number of buckets indicated by expr and assigns the appropriate bucket number to each row. The buckets are numbered 1 through expr. The expr value must resolve to a positive constant for each partition. Oracle Database expects an integer, and if expr is a noninteger constant, then Oracle truncates the value to an integer. The return value is NUMBER.
    The number of rows in the buckets can differ by at most 1. The remainder values (the remainder of number of rows divided by buckets) are distributed one for each bucket, starting with bucket 1.
    If expr is greater than the number of rows, then a number of buckets equal to the number of rows will be filled, and the remaining buckets will be empty.
    You cannot nest analytic functions by using NTILE or any other analytic function for expr. However, you can use other built-in function expressions for expr.

    The following example divides into 4 buckets the values in the salary column of the oe.employees table from Department 100. The salary column has 6 values in this department, so the two extra values (the remainder of 6 / 4) are allocated to buckets 1 and 2, which therefore have one more value than buckets 3 or 4.
    SELECT last_name, salary, NTILE(4) OVER (ORDER BY salary DESC) AS quartile
      FROM employees
      WHERE department_id = 100
      ORDER BY last_name, salary, quartile;
    
    LAST_NAME                     SALARY   QUARTILE
    ------------------------- ---------- ----------
    Chen                            8200          2
    Faviet                          9000          1
    Greenberg                      12008          1
    Popp                            6900          4
    Sciarra                         7700          3
    Urman                           7800          2
    

    Oracle Analytical Function NTH_VALUE With Example

    NTH_VALUE
    -------------------

    NTH_VALUE returns the measure_expr value of the nth row in the window defined by the analytic_clause. The returned value has the data type of the measure_expr.
    • {RESPECT | IGNORE} NULLS determines whether null values of measure_expr are included in or eliminated from the calculation. The default is RESPECT NULLS.
    • n determines the nth row for which the measure value is to be returned. n can be a constant, bind variable, column, or an expression involving them, as long as it resolves to a positive integer. The function returns NULL if the data source window has fewer than n rows. If n is null, then the function returns an error.
    • FROM {FIRST | LAST} determines whether the calculation begins at the first or last row of the window. The default is FROM FIRST.
    If you omit the windowing_clause of the analytic_clause, it defaults to RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. This default sometimes returns an unexpected value for NTH_VALUE ... FROM LAST ... , because the last value in the window is at the bottom of the window, which is not fixed. It keeps changing as the current row changes. For expected results, specify the windowing_clause as RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING. Alternatively, you can specify the windowing_clause as RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING.

    The following example shows the minimum amount_sold value for the second channel_id in ascending order for each prod_id between 13 and 16:
    SELECT prod_id, channel_id, MIN(amount_sold),
        NTH_VALUE(MIN(amount_sold), 2) OVER (PARTITION BY prod_id ORDER BY channel_id
        ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) nv
      FROM sales
      WHERE prod_id BETWEEN 13 and 16
      GROUP BY prod_id, channel_id;
    
       PROD_ID CHANNEL_ID MIN(AMOUNT_SOLD)         NV
    ---------- ---------- ---------------- ----------
            13          2           907.34      906.2
            13          3            906.2      906.2
            13          4           842.21      906.2
            14          2          1015.94    1036.72
            14          3          1036.72    1036.72
            14          4           935.79    1036.72
            15          2           871.19     871.19
            15          3           871.19     871.19
            15          4           871.19     871.19
            16          2           266.84     266.84
            16          3           266.84     266.84
            16          4           266.84     266.84
            16          9            11.99     266.84
    
    13 rows selected.
    

    Oracle Analytical Function MIN With Example

    MIN
    -------

    MIN returns minimum value of expr. You can use it as an aggregate or analytic function.

    Aggregate Example
    The following statement returns the earliest hire date in the hr.employees table:
    SELECT MIN(hire_date) "Earliest"
      FROM employees;
     
    Earliest
    ---------
    13-JAN-01
    
    Analytic Example
    The following example determines, for each employee, the employees who were hired on or before the same date as the employee. It then determines the subset of employees reporting to the same manager as the employee, and returns the lowest salary in that subset.
    SELECT manager_id, last_name, hire_date, salary,
           MIN(salary) OVER(PARTITION BY manager_id ORDER BY hire_date
             RANGE UNBOUNDED PRECEDING) AS p_cmin
      FROM employees
      ORDER BY manager_id, last_name, hire_date, salary;
    
    MANAGER_ID LAST_NAME                 HIRE_DATE     SALARY     P_CMIN
    ---------- ------------------------- --------- ---------- ----------
           100 Cambrault                 15-OCT-07      11000       6500
           100 De Haan                   13-JAN-01      17000      17000
           100 Errazuriz                 10-MAR-05      12000       7900
           100 Fripp                     10-APR-05       8200       7900
           100 Hartstein                 17-FEB-04      13000       7900
           100 Kaufling                  01-MAY-03       7900       7900
           100 Kochhar                   21-SEP-05      17000       7900
           100 Mourgos                   16-NOV-07       5800       5800
           100 Partners                  05-JAN-05      13500       7900
           100 Raphaely                  07-DEC-02      11000      11000
           100 Russell                   01-OCT-04      14000       7900
    
    . . .
    

    Oracle Analytical Function MAX With Example

    MAX
    ---------

    MAX returns maximum value of expr. You can use it as an aggregate or analytic function.

    The following example determines the highest salary in the hr.employees table:
    SELECT MAX(salary) "Maximum"
      FROM employees;
     
       Maximum
    ----------
         24000
    
    Analytic Examples
    The following example calculates, for each employee, the highest salary of the employees reporting to the same manager as the employee.
    SELECT manager_id, last_name, salary,
           MAX(salary) OVER (PARTITION BY manager_id) AS mgr_max
      FROM employees
      ORDER BY manager_id, last_name, salary;
    
    MANAGER_ID LAST_NAME                     SALARY    MGR_MAX
    ---------- ------------------------- ---------- ----------
           100 Cambrault                      11000      17000
           100 De Haan                        17000      17000
           100 Errazuriz                      12000      17000
           100 Fripp                           8200      17000
           100 Hartstein                      13000      17000
           100 Kaufling                        7900      17000
           100 Kochhar                        17000      17000
    . . .
    
    If you enclose this query in the parent query with a predicate, then you can determine the employee who makes the highest salary in each department:
    SELECT manager_id, last_name, salary
      FROM (SELECT manager_id, last_name, salary, 
                   MAX(salary) OVER (PARTITION BY manager_id) AS rmax_sal
              FROM employees)
      WHERE salary = rmax_sal
      ORDER BY manager_id, last_name, salary;
    
    MANAGER_ID LAST_NAME                     SALARY
    ---------- ------------------------- ----------
           100 De Haan                        17000
           100 Kochhar                        17000
           101 Greenberg                      12008
           101 Higgins                        12008
           102 Hunold                          9000
           103 Ernst                           6000
           108 Faviet                          9000
           114 Khoo                            3100
           120 Nayer                           3200
           120 Taylor                          3200
           121 Sarchand                        4200
           122 Chung                           3800
           123 Bell                            4000
           124 Rajs                            3500
           145 Tucker                         10000
           146 King                           10000
           147 Vishney                        10500
           148 Ozer                           11500
           149 Abel                           11000
           201 Fay                             6000
           205 Gietz                           8300
               King                           24000
    

    Oracle Analytical Function LISTAGG With Example

    LISTAGG
    --------------

    For a specified measure, LISTAGG orders data within each group specified in the ORDER BY clause and then concatenates the values of the measure column.
    • As a single-set aggregate function, LISTAGG operates on all rows and returns a single output row.
    • As a group-set aggregate, the function operates on and returns an output row for each group defined by the GROUP BY clause.
    • As an analytic function, LISTAGG partitions the query result set into groups based on one or more expression in the query_partition_clause.
    The arguments to the function are subject to the following rules:
    • The measure_expr can be any expression. Null values in the measure column are ignored.
    • The delimiter_expr designates the string that is to separate the measure values. This clause is optional and defaults to NULL.
    • The order_by_clause determines the order in which the concatenated values are returned. The function is deterministic only if the ORDER BY column list achieved unique ordering.
    The return data type is RAW if the measure column is RAW; otherwise the return value is VARCHAR2.

    The following single-set aggregate example lists all of the employees in Department 30 in the hr.employees table, ordered by hire date and last name:
    SELECT LISTAGG(last_name, '; ')
             WITHIN GROUP (ORDER BY hire_date, last_name) "Emp_list",
           MIN(hire_date) "Earliest"
      FROM employees
      WHERE department_id = 30;
    
    Emp_list                                                     Earliest
    ------------------------------------------------------------ ---------
    Raphaely; Khoo; Tobias; Baida; Himuro; Colmenares            07-DEC-02
    
    The following group-set aggregate example lists, for each department ID in the hr.employees table, the employees in that department in order of their hire date:
    SELECT department_id "Dept.",
           LISTAGG(last_name, '; ') WITHIN GROUP (ORDER BY hire_date) "Employees"
      FROM employees
      GROUP BY department_id
      ORDER BY department_id;
    
    Dept. Employees
    ------ ------------------------------------------------------------
        10 Whalen
        20 Hartstein; Fay
        30 Raphaely; Khoo; Tobias; Baida; Himuro; Colmenares
        40 Mavris
        50 Kaufling; Ladwig; Rajs; Sarchand; Bell; Mallin; Weiss; Davie
           s; Marlow; Bull; Everett; Fripp; Chung; Nayer; Dilly; Bissot
           ; Vollman; Stiles; Atkinson; Taylor; Seo; Fleaur; Matos; Pat
           el; Walsh; Feeney; Dellinger; McCain; Vargas; Gates; Rogers;
            Mikkilineni; Landry; Cabrio; Jones; Olson; OConnell; Sulliv
           an; Mourgos; Gee; Perkins; Grant; Geoni; Philtanker; Markle
        60 Austin; Hunold; Pataballa; Lorentz; Ernst
        70 Baer
    . . .
    
    Analytic Example
    The following analytic example shows, for each employee hired earlier than September 1, 2003, the employee's department, hire date, and all other employees in that department also hired before September 1, 2003:
    SELECT department_id "Dept", hire_date "Date", last_name "Name",
           LISTAGG(last_name, '; ') WITHIN GROUP (ORDER BY hire_date, last_name)
             OVER (PARTITION BY department_id) as "Emp_list"
      FROM employees
      WHERE hire_date < '01-SEP-2003'
      ORDER BY "Dept", "Date", "Name";
    
     Dept Date      Name            Emp_list
    ----- --------- --------------- ---------------------------------------------
       30 07-DEC-02 Raphaely        Raphaely; Khoo
       30 18-MAY-03 Khoo            Raphaely; Khoo
       40 07-JUN-02 Mavris          Mavris
       50 01-MAY-03 Kaufling        Kaufling; Ladwig
       50 14-JUL-03 Ladwig          Kaufling; Ladwig
       70 07-JUN-02 Baer            Baer
       90 13-JAN-01 De Haan         De Haan; King
       90 17-JUN-03 King            De Haan; King
      100 16-AUG-02 Faviet          Faviet; Greenberg
      100 17-AUG-02 Greenberg       Faviet; Greenberg
      110 07-JUN-02 Gietz           Gietz; Higgins
      110 07-JUN-02 Higgins         Gietz; Higgins
    

    Oracle Analytical Function LEAD With Example

    LEAD
    ---------

    LEAD is an analytic function. It provides access to more than one row of a table at the same time without a self join. Given a series of rows returned from a query and a position of the cursor, LEAD provides access to a row at a given physical offset beyond that position.
    If you do not specify offset, then its default is 1. The optional default value is returned if the offset goes beyond the scope of the table. If you do not specify default, then its default value is null.
    {RESPECT | IGNORE} NULLS determines whether null values of value_expr are included in or eliminated from the calculation. The default is RESPECT NULLS.
    You cannot nest analytic functions by using LEAD or any other analytic function for value_expr. However, you can use other built-in function expressions for value_expr.

    The following example provides, for each employee in the employees table, the hire date of the employee hired just after:
    SELECT last_name, hire_date, 
           LEAD(hire_date, 1) OVER (ORDER BY hire_date) AS "NextHired" 
      FROM employees
      WHERE department_id = 30
      ORDER BY last_name, hire_date;
    
    LAST_NAME                 HIRE_DATE NextHired
    ------------------------- --------- ---------
    Baida                     24-DEC-05 15-NOV-06
    Colmenares                10-AUG-07
    Himuro                    15-NOV-06 10-AUG-07
    Khoo                      18-MAY-03 24-JUL-05
    Raphaely                  07-DEC-02 18-MAY-03
    Tobias                    24-JUL-05 24-DEC-05
    

    Oracle Analytical Function LAST_VALUE With Example

    LAST_VALUE
    --------------------

    LAST_VALUE is an analytic function that is useful for data densification. It returns the last value in an ordered set of values

    The following example returns, for each row, the hire date of the employee earning the highest salary:
    SELECT last_name, salary, hire_date,
           LAST_VALUE(hire_date)
             OVER (ORDER BY salary ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED
                   FOLLOWING) AS lv
      FROM (SELECT * FROM employees
              WHERE department_id = 90
              ORDER BY hire_date);
    
    LAST_NAME           SALARY HIRE_DATE LV
    --------------- ---------- --------- ---------
    De Haan              17000 13-JAN-01 17-JUN-03
    Kochhar              17000 21-SEP-05 17-JUN-03
    King                 24000 17-JUN-03 17-JUN-03
    
    This example illustrates the nondeterministic nature of the LAST_VALUE function. Kochhar and De Haan have the same salary, so they are in adjacent rows. Kochhar appears first because the rows in the subquery are ordered by hire_date. However, if the rows are ordered by hire_date in descending order, as in the next example, then the function returns a different value:
    SELECT last_name, salary, hire_date,
           LAST_VALUE(hire_date)
             OVER (ORDER BY salary ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED
                   FOLLOWING) AS lv
      FROM (SELECT * FROM employees
              WHERE department_id = 90 
              ORDER BY hire_date DESC);
    
    LAST_NAME           SALARY HIRE_DATE LV
    --------------- ---------- --------- ---------
    Kochhar              17000 21-SEP-05 17-JUN-03
    De Haan              17000 13-JAN-01 17-JUN-03
    King                 24000 17-JUN-03 17-JUN-03
    
    The following two examples show how to make the LAST_VALUE function deterministic by ordering on a unique key. By ordering within the function by both salary and hire_date, you can ensure the same result regardless of the ordering in the subquery.
    SELECT last_name, salary, hire_date,
           LAST_VALUE(hire_date)
             OVER (ORDER BY salary, hire_date ROWS BETWEEN UNBOUNDED PRECEDING AND
                   UNBOUNDED FOLLOWING) AS lv
      FROM (SELECT * FROM employees
              WHERE department_id = 90 
              ORDER BY hire_date)
      ORDER BY last_name, salary, hire_date;
    
    LAST_NAME           SALARY HIRE_DATE LV
    --------------- ---------- --------- ---------
    De Haan              17000 13-JAN-01 17-JUN-03
    King                 24000 17-JUN-03 17-JUN-03
    Kochhar              17000 21-SEP-05 17-JUN-03
    
    SELECT last_name, salary, hire_date,
           LAST_VALUE(hire_date)
             OVER (ORDER BY salary, hire_date ROWS BETWEEN UNBOUNDED PRECEDING AND
                   UNBOUNDED FOLLOWING) AS lv
      FROM (SELECT * FROM employees
              WHERE department_id = 90 
              ORDER BY hire_date DESC)
      ORDER BY last_name, salary, hire_date;
    
    LAST_NAME           SALARY HIRE_DATE LV
    --------------- ---------- --------- ---------
    De Haan              17000 13-JAN-01 17-JUN-03
    King                 24000 17-JUN-03 17-JUN-03
    Kochhar              17000 21-SEP-05 17-JUN-03
    
    When you use a logical offset (RANGE instead of ROWS), the function is deterministic. When duplicates are found for the ORDER BY expression, the LAST_VALUE is the highest value of expr:
    SELECT last_name, salary, hire_date,
           LAST_VALUE(hire_date)
             OVER (ORDER BY salary RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED
                   FOLLOWING) AS lv
      FROM (SELECT * FROM employees
              WHERE department_id = 90
              ORDER BY hire_date DESC);
     
    LAST_NAME                     SALARY HIRE_DATE LV
    ------------------------- ---------- --------- ---------
    De Haan                        17000 13-JAN-01 17-JUN-03
    Kochhar                        17000 21-SEP-05 17-JUN-03
    King                           24000 17-JUN-03 17-JUN-03
    

    Oracle Analytical Function LAST With Example

    LAST
    --------

    FIRST and LAST are very similar functions. Both are aggregate and analytic functions that operate on a set of values from a set of rows that rank as the FIRST or LAST with respect to a given sorting specification. If only one row ranks as FIRST or LAST, then the aggregate operates on the set with only one element.

    The following example returns, within each department of the sample table hr.employees, the minimum salary among the employees who make the lowest commission and the maximum salary among the employees who make the highest commission:

    SELECT department_id,
           MIN(salary) KEEP (DENSE_RANK FIRST ORDER BY commission_pct) "Worst",
           MAX(salary) KEEP (DENSE_RANK LAST ORDER BY commission_pct) "Best"
      FROM employees
      GROUP BY department_id
      ORDER BY department_id;
    
    DEPARTMENT_ID      Worst       Best
    ------------- ---------- ----------
               10       4400       4400
               20       6000      13000
               30       2500      11000
               40       6500       6500
               50       2100       8200
               60       4200       9000
               70      10000      10000
               80       6100      14000
               90      17000      24000
              100       6900      12008
              110       8300      12008
                        7000       7000
    
    Analytic Example
    The next example makes the same calculation as the previous example but returns the result for each employee within the department:
    SELECT last_name, department_id, salary,
           MIN(salary) KEEP (DENSE_RANK FIRST ORDER BY commission_pct)
             OVER (PARTITION BY department_id) "Worst",
           MAX(salary) KEEP (DENSE_RANK LAST ORDER BY commission_pct)
             OVER (PARTITION BY department_id) "Best"
       FROM employees
       ORDER BY department_id, salary, last_name;
    
    LAST_NAME           DEPARTMENT_ID     SALARY      Worst       Best
    ------------------- ------------- ---------- ---------- ----------
    Whalen                         10       4400       4400       4400
    Fay                            20       6000       6000      13000
    Hartstein                      20      13000       6000      13000
    . . .
    Gietz                         110       8300       8300      12008
    Higgins                       110      12008       8300      12008
    Grant                                   7000       7000       7000
    

    Oracle Analytical Function LAG With Example

    LAG
    -------

    LAG is an analytic function. It provides access to more than one row of a table at the same time without a self join. Given a series of rows returned from a query and a position of the cursor, LAG provides access to a row at a given physical offset prior to that position.
    For the optional offset argument, specify an integer that is greater than zero. If you do not specify offset, then its default is 1. The optional default value is returned if the offset goes beyond the scope of the window. If you do not specify default, then its default is null.

    The following example provides, for each salesperson in the employees table, the salary of the employee hired just before:
    SELECT last_name, hire_date, salary,
           LAG(salary, 1, 0) OVER (ORDER BY hire_date) AS prev_sal
      FROM employees
      WHERE job_id = 'PU_CLERK'
      ORDER BY last_name, hire_date, salary;
       
    LAST_NAME                 HIRE_DATE     SALARY   PREV_SAL
    ------------------------- --------- ---------- ----------
    Baida                     24-DEC-05       2900       2800
    Colmenares                10-AUG-07       2500       2600
    Himuro                    15-NOV-06       2600       2900
    Khoo                      18-MAY-03       3100          0
    Tobias                    24-JUL-05       2800       3100
    

    Oracle Analytical Function FIRST_VALUE With Example

    FIRST_VALUE
    ----------------------

    FIRST_VALUE is an analytic function. It returns the first value in an ordered set of values. If the first value in the set is null, then the function returns NULL unless you specify IGNORE NULLS. This setting is useful for data densification.

    The following example selects, for each employee in Department 90, the name of the employee with the lowest salary.
    SELECT department_id, last_name, salary,
           FIRST_VALUE(last_name)
             OVER (ORDER BY salary ASC ROWS UNBOUNDED PRECEDING) AS lowest_sal
      FROM (SELECT * FROM employees
              WHERE department_id = 90
              ORDER BY employee_id)
      ORDER BY last_name;
    
    DEPARTMENT_ID LAST_NAME                     SALARY LOWEST_SAL
    ------------- ------------------------- ---------- -------------------------
               90 De Haan                        17000 Kochhar
               90 King                           24000 Kochhar
               90 Kochhar                        17000 Kochhar
    
    The example illustrates the nondeterministic nature of the FIRST_VALUE function. Kochhar and DeHaan have the same salary, so are in adjacent rows. Kochhar appears first because the rows returned by the subquery are ordered by employee_id. However, if the rows returned by the subquery are ordered by employee_id in descending order, as in the next example, then the function returns a different value:
    SELECT department_id, last_name, salary,
           FIRST_VALUE(last_name)
             OVER (ORDER BY salary ASC ROWS UNBOUNDED PRECEDING) AS fv
      FROM (SELECT * FROM employees
              WHERE department_id = 90
              ORDER by employee_id DESC)
      ORDER BY last_name;
    
    DEPARTMENT_ID LAST_NAME                     SALARY FV
    ------------- ------------------------- ---------- -------------------------
               90 De Haan                        17000 De Haan
               90 King                           24000 De Haan
               90 Kochhar                        17000 De Haan
    
    The following example shows how to make the FIRST_VALUE function deterministic by ordering on a unique key.
    SELECT department_id, last_name, salary, hire_date,
           FIRST_VALUE(last_name)
             (ORDER BY salary ASC, hire_date ROWS UNBOUNDED PRECEDING) AS fv
      FROM (SELECT * FROM employees 
              WHERE department_id = 90
              ORDER BY employee_id DESC)
      ORDER BY last_name;
    
    DEPARTMENT_ID LAST_NAME           SALARY HIRE_DATE FV
    ------------- --------------- ---------- --------- -------------------------
               90 De Haan              17000 13-JAN-01 De Haan
               90 King                 24000 17-JUN-03 De Haan
               90 Kochhar              17000 21-SEP-05 De Haan
    
    When you use a logical offset (RANGE instead of ROWS), the function is deterministic. When duplicates are found for the ORDER BY expression, the FIRST_VALUE is the lowest value of expr:
    SELECT department_id, last_name, salary,
           FIRST_VALUE(last_name)
             OVER (ORDER BY salary ASC RANGE UNBOUNDED PRECEDING) AS lowest_sal
      FROM (SELECT * FROM employees
              WHERE department_id = 90
              ORDER BY employee_id);
     
    DEPARTMENT_ID LAST_NAME                     SALARY LOWEST_SAL
    ------------- ------------------------- ---------- -------------------------
               90 De Haan                        17000 De Haan
               90 Kochhar                        17000 De Haan
               90 King                           24000 De Haan
    

    Oracle Analytical Function FIRST With Example

    FIRST
    ---------

    FIRST and LAST are very similar functions. Both are aggregate and analytic functions that operate on a set of values from a set of rows that rank as the FIRST or LAST with respect to a given sorting specification. If only one row ranks as FIRST or LAST, then the aggregate operates on the set with only one element.
    If you omit the OVER clause, then the FIRST and LAST functions are treated as aggregate functions. You can use these functions as analytic functions by specifying the OVER clause. The query_partition_clause is the only part of the OVER clause valid with these functions. If you include the OVER clause but omit the query_partition_clause, then the function is treated as an analytic function, but the window defined for analysis is the entire table.
    These functions take as an argument any numeric data type or any nonnumeric data type that can be implicitly converted to a numeric data type. The function returns the same data type as the numeric data type of the argument.
    When you need a value from the first or last row of a sorted group, but the needed value is not the sort key, the FIRST and LAST functions eliminate the need for self-joins or views and enable better performance.
    • The aggregate_function argument is any one of the MIN, MAX, SUM, AVG, COUNT, VARIANCE, or STDDEV functions. It operates on values from the rows that rank either FIRST or LAST. If only one row ranks as FIRST or LAST, then the aggregate operates on a singleton (nonaggregate) set.
    • The KEEP keyword is for semantic clarity. It qualifies aggregate_function, indicating that only the FIRST or LAST values of aggregate_function will be returned.
    • DENSE_RANK FIRST or DENSE_RANK LAST indicates that Oracle Database will aggregate over only those rows with the minimum (FIRST) or the maximum (LAST) dense rank (also called olympic rank).

    The following example returns, within each department of the sample table hr.employees, the minimum salary among the employees who make the lowest commission and the maximum salary among the employees who make the highest commission:
    SELECT department_id,
           MIN(salary) KEEP (DENSE_RANK FIRST ORDER BY commission_pct) "Worst",
           MAX(salary) KEEP (DENSE_RANK LAST ORDER BY commission_pct) "Best"
      FROM employees
      GROUP BY department_id
      ORDER BY department_id;
    
    DEPARTMENT_ID      Worst       Best
    ------------- ---------- ----------
               10       4400       4400
               20       6000      13000
               30       2500      11000
               40       6500       6500
               50       2100       8200
               60       4200       9000
               70      10000      10000
               80       6100      14000
               90      17000      24000
              100       6900      12008
              110       8300      12008
                        7000       7000
    
    Analytic Example
    The next example makes the same calculation as the previous example but returns the result for each employee within the department:
    SELECT last_name, department_id, salary,
           MIN(salary) KEEP (DENSE_RANK FIRST ORDER BY commission_pct)
             OVER (PARTITION BY department_id) "Worst",
           MAX(salary) KEEP (DENSE_RANK LAST ORDER BY commission_pct)
             OVER (PARTITION BY department_id) "Best"
       FROM employees
       ORDER BY department_id, salary, last_name;
    
    LAST_NAME           DEPARTMENT_ID     SALARY      Worst       Best
    ------------------- ------------- ---------- ---------- ----------
    Whalen                         10       4400       4400       4400
    Fay                            20       6000       6000      13000
    Hartstein                      20      13000       6000      13000
    . . .
    Gietz                         110       8300       8300      12008
    Higgins                       110      12008       8300      12008
    Grant                                   7000       7000       7000
    

    Oracle Analytical Function DENSE_RANK With Example

    DENSE_RANK
    ----------------------

    DENSE_RANK computes the rank of a row in an ordered group of rows and returns the rank as a NUMBER. The ranks are consecutive integers beginning with 1. The largest rank value is the number of unique values returned by the query. Rank values are not skipped in the event of ties. Rows with equal values for the ranking criteria receive the same rank. This function is useful for top-N and bottom-N reporting.
    This function accepts as arguments any numeric data type and returns NUMBER.
    • As an aggregate function, DENSE_RANK calculates the dense rank of a hypothetical row identified by the arguments of the function with respect to a given sort specification. The arguments of the function must all evaluate to constant expressions within each aggregate group, because they identify a single row within each group. The constant argument expressions and the expressions in the order_by_clause of the aggregate match by position. Therefore, the number of arguments must be the same and types must be compatible.
    • As an analytic function, DENSE_RANK computes the rank of each row returned from a query with respect to the other rows, based on the values of the value_exprs in the order_by_clause.

    The following example computes the ranking of a hypothetical employee with the salary $15,500 and a commission of 5% in the sample table oe.employees:
    SELECT DENSE_RANK(15500, .05) WITHIN GROUP 
      (ORDER BY salary DESC, commission_pct) "Dense Rank" 
      FROM employees;
    
    Dense Rank
    ----------
             3
    
    Analytic Example
    The following statement ranks the employees in the sample hr schema in department 60 based on their salaries. Identical salary values receive the same rank. However, no rank values are skipped. Compare this example with the analytic example for RANK.
    SELECT department_id, last_name, salary,
           DENSE_RANK() OVER (PARTITION BY department_id ORDER BY salary) DENSE_RANK
      FROM employees WHERE department_id = 60
      ORDER BY DENSE_RANK, last_name;
     
    DEPARTMENT_ID LAST_NAME                     SALARY DENSE_RANK
    ------------- ------------------------- ---------- ----------
               60 Lorentz                         4200          1
               60 Austin                          4800          2
               60 Pataballa                       4800          2
               60 Ernst                           6000          3
               60 Hunold                          9000          4
    

    Oracle Analytical Function CUME_DIST With Example

    CUME_DIST
    ------------------

    CUME_DIST calculates the cumulative distribution of a value in a group of values. The range of values returned by CUME_DIST is >0 to <=1. Tie values always evaluate to the same cumulative distribution value.
    This function takes as arguments any numeric data type or any nonnumeric data type that can be implicitly converted to a numeric data type. Oracle Database determines the argument with the highest numeric precedence, implicitly converts the remaining arguments to that data type, makes the calculation, and returns NUMBER.

    The following example calculates the cumulative distribution of a hypothetical employee with a salary of $15,500 and commission rate of 5% among the employees in the sample table oe.employees:
    SELECT CUME_DIST(15500, .05) WITHIN GROUP
      (ORDER BY salary, commission_pct) "Cume-Dist of 15500" 
      FROM employees;
    
    Cume-Dist of 15500
    ------------------
            .972222222
    
    Analytic Example
    The following example calculates the salary percentile for each employee in the purchasing division. For example, 40% of clerks have salaries less than or equal to Himuro.
    SELECT job_id, last_name, salary, CUME_DIST() 
      OVER (PARTITION BY job_id ORDER BY salary) AS cume_dist
      FROM employees
      WHERE job_id LIKE 'PU%'
      ORDER BY job_id, last_name, salary, cume_dist;
    
    JOB_ID     LAST_NAME                     SALARY  CUME_DIST
    ---------- ------------------------- ---------- ----------
    PU_CLERK   Baida                           2900         .8
    PU_CLERK   Colmenares                      2500         .2
    PU_CLERK   Himuro                          2600         .4
    PU_CLERK   Khoo                            3100          1
    PU_CLERK   Tobias                          2800         .6
    PU_MAN     Raphaely                       11000          1
    

    Oracle Analytical Function COVAR_SAMP With Example

    COVAR_SAMP
    ---------------------

    COVAR_SAMP returns the sample covariance of a set of number pairs. You can use it as an aggregate or analytic function.
    This function takes as arguments any numeric data type or any nonnumeric data type that can be implicitly converted to a numeric data type. Oracle determines the argument with the highest numeric precedence, implicitly converts the remaining arguments to that data type, and returns that data type.

    Refer Example of COVAR_POP

    Oracle Analytical Function COUNT With Example

    COVAR_POP
    -------------------

    COVAR_POP returns the population covariance of a set of number pairs. You can use it as an aggregate or analytic function.
    This function takes as arguments any numeric data type or any nonnumeric data type that can be implicitly converted to a numeric data type. Oracle determines the argument with the highest numeric precedence, implicitly converts the remaining arguments to that data type, and returns that data type.

    The following example calculates the population covariance and sample covariance for time employed (SYSDATE - hire_date) and salary using the sample table hr.employees:
    SELECT job_id, 
           COVAR_POP(SYSDATE-hire_date, salary) AS covar_pop,
           COVAR_SAMP(SYSDATE-hire_date, salary) AS covar_samp
      FROM employees
      WHERE department_id in (50, 80)
      GROUP BY job_id
      ORDER BY job_id, covar_pop, covar_samp;
    
    JOB_ID       COVAR_POP  COVAR_SAMP
    ---------- ----------- -----------
    SA_MAN          660700      825875
    SA_REP      579988.466   600702.34
    SH_CLERK      212432.5  223613.158
    ST_CLERK     176577.25  185870.789
    ST_MAN          436092      545115
    
    Analytic Example
    The following example calculates cumulative sample covariance of the list price and minimum price of the products in the sample schema oe:
    SELECT product_id, supplier_id,
           COVAR_POP(list_price, min_price) 
             OVER (ORDER BY product_id, supplier_id)
             AS CUM_COVP,
           COVAR_SAMP(list_price, min_price)
             OVER (ORDER BY product_id, supplier_id)
             AS CUM_COVS 
      FROM product_information p
      WHERE category_id = 29
      ORDER BY product_id, supplier_id;
    
    PRODUCT_ID SUPPLIER_ID   CUM_COVP   CUM_COVS
    ---------- ----------- ---------- ----------
          1774      103088          0
          1775      103087    1473.25     2946.5
          1794      103096 1702.77778 2554.16667
          1825      103093    1926.25 2568.33333
          2004      103086     1591.4    1989.25
          2005      103086     1512.5       1815
          2416      103088 1475.97959 1721.97619
    . . .
    

    Oracle Analytical Function COUNT With Example

    COUNT
    -----------

    COUNT returns the number of rows returned by the query. You can use it as an aggregate or analytic function.
    If you specify DISTINCT, then you can specify only the query_partition_clause of the analytic_clause. The order_by_clause and windowing_clause are not allowed.
    If you specify expr, then COUNT returns the number of rows where expr is not null. You can count either all rows, or only distinct values of expr.
    If you specify the asterisk (*), then this function returns all rows, including duplicates and nulls. COUNT never returns null.


    The following examples use COUNT as an aggregate function:
    SELECT COUNT(*) "Total"
      FROM employees;
    
         Total
    ----------
           107
    
    SELECT COUNT(*) "Allstars"
      FROM employees
      WHERE commission_pct > 0;
    
     Allstars
    ---------
           35
    
    SELECT COUNT(commission_pct) "Count"
      FROM employees;
    
         Count
    ----------
            35
    
    SELECT COUNT(DISTINCT manager_id) "Managers"
      FROM employees;
    
      Managers
    ----------
            18
    
    Analytic Example
    The following example calculates, for each employee in the employees table, the moving count of employees earning salaries in the range 50 less than through 150 greater than the employee's salary.
    SELECT last_name, salary,
           COUNT(*) OVER (ORDER BY salary RANGE BETWEEN 50 PRECEDING AND
                          150 FOLLOWING) AS mov_count
      FROM employees
      ORDER BY salary, last_name;
    
    LAST_NAME                     SALARY  MOV_COUNT
    ------------------------- ---------- ----------
    Olson                           2100          3
    Markle                          2200          2
    Philtanker                      2200          2
    Gee                             2400          8
    Landry                          2400          8
    Colmenares                      2500         10
    Marlow                          2500         10
    Patel                           2500         10
    . . .
    

    Oracle Analytical Function CORR With Example

    CORR
    ----------

    CORR returns the coefficient of correlation of a set of number pairs. You can use it as an aggregate or analytic function.
    This function takes as arguments any numeric data type or any nonnumeric data type that can be implicitly converted to a numeric data type. Oracle determines the argument with the highest numeric precedence, implicitly converts the remaining arguments to that data type, and returns that data type.

    The following example calculates the coefficient of correlation between the list prices and minimum prices of products by weight class in the sample table oe.product_information:
    SELECT weight_class, CORR(list_price, min_price) "Correlation"
      FROM product_information
      GROUP BY weight_class
      ORDER BY weight_class, "Correlation";
    
    WEIGHT_CLASS Correlation
    ------------ -----------
               1  .999149795
               2  .999022941
               3  .998484472
               4  .999359909
               5  .999536087
    
    Analytic Example
    The following example shows the correlation between duration at the company and salary by the employee's position. The result set shows the same correlation for each employee in a given job:
    SELECT employee_id, job_id, 
           TO_CHAR((SYSDATE - hire_date) YEAR TO MONTH ) "Yrs-Mns",     salary, 
           CORR(SYSDATE-hire_date, salary)
           OVER(PARTITION BY job_id) AS "Correlation"
      FROM employees
      WHERE department_id in (50, 80)
      ORDER BY job_id, employee_id;
    
    EMPLOYEE_ID JOB_ID     Yrs-Mns     SALARY Correlation
    ----------- ---------- ------- ---------- -----------
            145 SA_MAN     +04-09       14000  .912385598
            146 SA_MAN     +04-06       13500  .912385598
            147 SA_MAN     +04-04       12000  .912385598
            148 SA_MAN     +01-08       11000  .912385598
            149 SA_MAN     +01-05       10500  .912385598
            150 SA_REP     +04-05       10000   .80436755
            151 SA_REP     +04-03        9500   .80436755
            152 SA_REP     +03-10        9000   .80436755
            153 SA_REP     +03-03        8000   .80436755
            154 SA_REP     +02-07        7500   .80436755
            155 SA_REP     +01-07        7000   .80436755
    . . .
    

    Oracle Analytical Function AVG With Example

    AVG
    -------

    AVG returns average value of expr.

    This function takes as an argument any numeric data type or any nonnumeric data type that can be implicitly converted to a numeric data type. The function returns the same data type as the numeric data type of the argument.

    The following example calculates the average salary of all employees in the hr.employees table:
    SELECT AVG(salary) "Average"
      FROM employees;
    
           Average
    --------------
        6461.83178
    
    Analytic Example
    The following example calculates, for each employee in the employees table, the average salary of the employees reporting to the same manager who were hired in the range just before through just after the employee:
    SELECT manager_id, last_name, hire_date, salary,
           AVG(salary) OVER (PARTITION BY manager_id ORDER BY hire_date 
      ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS c_mavg
      FROM employees
      ORDER BY manager_id, hire_date, salary;
    
    MANAGER_ID LAST_NAME                 HIRE_DATE     SALARY     C_MAVG
    ---------- ------------------------- --------- ---------- ----------
           100 De Haan                   13-JAN-01      17000      14000
           100 Raphaely                  07-DEC-02      11000 11966.6667
           100 Kaufling                  01-MAY-03       7900 10633.3333
           100 Hartstein                 17-FEB-04      13000 9633.33333
           100 Weiss                     18-JUL-04       8000 11666.6667
           100 Russell                   01-OCT-04      14000 11833.3333
           100 Partners                  05-JAN-05      13500 13166.6667
           100 Errazuriz                 10-MAR-05      12000 11233.3333
    . . .
    

    Oracle Analytical Functions with Example

    Introduction

    Probably the easiest way to understand analytic functions is to start by looking at aggregate functions. An aggregate function, as the name suggests, aggregates data from several rows into a single result row. For example, we might use the AVG aggregate function to give us an average of all the employee salaries in the EMP table.
    SELECT AVG(sal)
    FROM   emp;
    
      AVG(SAL)
    ----------
    2073.21429
    
    SQL>
    The GROUP BY clause allows us to apply aggregate functions to subsets of rows. For example, we might want to display the average salary for each department.
    SELECT deptno, AVG(sal)
    FROM   emp
    GROUP BY deptno
    ORDER BY deptno;
    
        DEPTNO   AVG(SAL)
    ---------- ----------
     10 2916.66667
     20  2175
     30 1566.66667
    
    SQL>
    In both cases, the aggregate function reduces the number of rows returned by the query.
    Analytic functions also operate on subsets of rows, similar to aggregate functions in GROUP BY queries, but they do not reduce the number of rows returned by the query. For example, the following query reports the salary for each employee, along with the average salary of the employees within the department.
    SELECT empno, deptno, sal,
           AVG(sal) OVER (PARTITION BY deptno) AS avg_dept_sal
    FROM   emp;
    
         EMPNO     DEPTNO        SAL AVG_DEPT_SAL
    ---------- ---------- ---------- ------------
          7782         10       2450   2916.66667
          7839         10       5000   2916.66667
          7934         10       1300   2916.66667
          7566         20       2975         2175
          7902         20       3000         2175
          7876         20       1100         2175
          7369         20        800         2175
          7788         20       3000         2175
          7521         30       1250   1566.66667
          7844         30       1500   1566.66667
          7499         30       1600   1566.66667
          7900         30        950   1566.66667
          7698         30       2850   1566.66667
          7654         30       1250   1566.66667
    
    
    SQL>
    This time AVG is an analytic function, operating on the group of rows defined by the contents of the OVER clause. This group of rows is known as a window, which is why analytic functions are sometimes referred to as window[ing] functions. Notice how the AVG function is still reporting the departmental average, like it did in the GROUP BY query, but the result is present in each row, rather than reducing the total number of rows returned. This is because analytic functions are performed on a result set after all join, WHERE, GROUP BY and HAVING clauses are complete, but before the final ORDER BY operation is performed.

    Analytic Function Syntax

    There are some variations in the syntax of the individual analytic functions, but the basic syntax for an analytic function is as follows.
    analytic_function([ arguments ]) OVER (analytic_clause)
    The analytic_clause breaks down into the following optional elements.
    [ query_partition_clause ] [ order_by_clause [ windowing_clause ] ]
    The sub-elements of the analytic_clause each have their own syntax diagrams, shown here. Rather than repeat the syntax diagrams, the following sections describe what each section of the analytic_clause is used for.

    query_partition_clause

    The query_partition_clause divides the result set into partitions, or groups, of data. The operation of the analytic function is restricted to the boundary imposed by these partitions, similar to the way a GROUP BY clause affects the action of an aggregate function. If the query_partition_clause is omitted, the whole result set is treated as a single partition. The following query uses an empty OVER clause, so the average presented is based on all the rows of the result set.
    SELECT empno, deptno, sal,
           AVG(sal) OVER () AS avg_sal
    FROM   emp;
    
         EMPNO     DEPTNO        SAL    AVG_SAL
    ---------- ---------- ---------- ----------
          7369         20        800 2073.21429
          7499         30       1600 2073.21429
          7521         30       1250 2073.21429
          7566         20       2975 2073.21429
          7654         30       1250 2073.21429
          7698         30       2850 2073.21429
          7782         10       2450 2073.21429
          7788         20       3000 2073.21429
          7839         10       5000 2073.21429
          7844         30       1500 2073.21429
          7876         20       1100 2073.21429
          7900         30        950 2073.21429
          7902         20       3000 2073.21429
          7934         10       1300 2073.21429
    
    SQL>
    If we change the OVER clause to include a query_partition_clause based on the department, the averages presented are specifically for the department the employee belongs too.
    SELECT empno, deptno, sal,
           AVG(sal) OVER (PARTITION BY deptno) AS avg_dept_sal
    FROM   emp;
    
         EMPNO     DEPTNO        SAL AVG_DEPT_SAL
    ---------- ---------- ---------- ------------
          7782         10       2450   2916.66667
          7839         10       5000   2916.66667
          7934         10       1300   2916.66667
          7566         20       2975         2175
          7902         20       3000         2175
          7876         20       1100         2175
          7369         20        800         2175
          7788         20       3000         2175
          7521         30       1250   1566.66667
          7844         30       1500   1566.66667
          7499         30       1600   1566.66667
          7900         30        950   1566.66667
          7698         30       2850   1566.66667
          7654         30       1250   1566.66667
    
    SQL>

    order_by_clause

    The order_by_clause is used to order rows, or siblings, within a partition. So if an analytic function is sensitive to the order of the siblings in a partition you should include an order_by_clause. The following query uses the FIRST_VALUE function to return the first salary reported in each department. Notice we have partitioned the result set by the department, but there is no order_by_clause.
    SELECT empno, deptno, sal, 
           FIRST_VALUE(sal IGNORE NULLS) OVER (PARTITION BY deptno) AS first_sal_in_dept
    FROM   emp;
    
         EMPNO     DEPTNO        SAL FIRST_SAL_IN_DEPT
    ---------- ---------- ---------- -----------------
          7782         10       2450              2450
          7839         10       5000              2450
          7934         10       1300              2450
          7566         20       2975              2975
          7902         20       3000              2975
          7876         20       1100              2975
          7369         20        800              2975
          7788         20       3000              2975
          7521         30       1250              1250
          7844         30       1500              1250
          7499         30       1600              1250
          7900         30        950              1250
          7698         30       2850              1250
          7654         30       1250              1250
    
    
    SQL>
    Now compare the values of the FIRST_SAL_IN_DEPT column when we include an order_by_clause to order the siblings by ascending salary.
    SELECT empno, deptno, sal, 
           FIRST_VALUE(sal IGNORE NULLS) OVER (PARTITION BY deptno ORDER BY sal ASC NULLS LAST) AS first_val_in_dept
    FROM   emp;
    
         EMPNO     DEPTNO        SAL FIRST_VAL_IN_DEPT
    ---------- ---------- ---------- -----------------
          7934         10       1300              1300
          7782         10       2450              1300
          7839         10       5000              1300
          7369         20        800               800
          7876         20       1100               800
          7566         20       2975               800
          7788         20       3000               800
          7902         20       3000               800
          7900         30        950               950
          7654         30       1250               950
          7521         30       1250               950
          7844         30       1500               950
          7499         30       1600               950
          7698         30       2850               950
    
    
    SQL>
    In this case the "ASC NULLS LAST" keywords are unnecessary as ASC is the default for an order_by_clause and NULLS LAST is the default for ASC orders. When ordering by DESC, the default is NULLS FIRST.
    It is important to understand how the order_by_clause affects display order. The order_by_clause is guaranteed to affect the order of the rows as they are processed by the analytic function, but it may not always affect the display order. As a result, you must always use a conventional ORDER BY clause in the query if display order is important. Do not rely on any implicit ordering done by the analytic function. Remember, the conventional ORDER BY clause is performed after the analytic processing, so it will always take precedence.

    windowing_clause

    We have seen previously the query_partition_clause controls the window, or group of rows, the analytic operates on. The windowing_clause gives some analytic functions a further degree of control over this window within the current partition. The windowing_clause is an extension of the order_by_clause and as such, it can only be used if an order_by_clause is present. The windowing_clause has two basic forms.
    RANGE BETWEEN start_point AND end_point
    ROWS BETWEEN start_point AND end_point
    Possible values for "start_point" and "end_point" are:
    • UNBOUNDED PRECEDING : The window starts at the first row of the partition. Only available for start points.
    • UNBOUNDED FOLLOWING : The window ends at the last row of the partition. Only available for end points.
    • CURRENT ROW : The window starts or ends at the current row. Can be used as start or end point.
    • value_expr PRECEDING : A physical or logical offset before the current row using a constant or expression that evaluates to a positive numerical value. When used with RANGE, it can also be an interval literal if the order_by_clause uses a DATE column.
    • value_expr FOLLOWING : As above, but an offset after the current row.
    The documentation states the start point must always be before the end point, but this is not true, as demonstrated by this rather silly, but valid, query.
    SELECT empno, deptno, sal, 
           AVG(sal) OVER (PARTITION BY deptno ORDER BY sal ROWS BETWEEN 0 PRECEDING AND 0 PRECEDING) AS avg_of_current_sal
    FROM   emp;
    
         EMPNO     DEPTNO        SAL AVG_OF_CURRENT_SAL
    ---------- ---------- ---------- ------------------
          7934         10       1300               1300
          7782         10       2450               2450
          7839         10       5000               5000
          7369         20        800                800
          7876         20       1100               1100
          7566         20       2975               2975
          7788         20       3000               3000
          7902         20       3000               3000
          7900         30        950                950
          7654         30       1250               1250
          7521         30       1250               1250
          7844         30       1500               1500
          7499         30       1600               1600
          7698         30       2850               2850
    
    
    SQL>
    In fact, the start point must be before or equal to the end point.
    For analytic functions that support the windowing_clause, the default action is "RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW". The following query is similar to one used previously to report the employee salary and average department salary, but now we have included an order_by_clause so we also get the default windowing_clause. Notice how the average salary is now calculated using only the employees from the same department up to and including the current row.
    SELECT empno, deptno, sal, 
           AVG(sal) OVER (PARTITION BY deptno ORDER BY sal) AS avg_dept_sal_sofar
    FROM   emp;
    
         EMPNO     DEPTNO        SAL AVG_DEPT_SAL_SOFAR
    ---------- ---------- ---------- ------------------
          7934         10       1300               1300
          7782         10       2450               1875
          7839         10       5000         2916.66667
          7369         20        800                800
          7876         20       1100                950
          7566         20       2975               1625
          7788         20       3000               2175
          7902         20       3000               2175
          7900         30        950                950
          7654         30       1250               1150
          7521         30       1250               1150
          7844         30       1500             1237.5
          7499         30       1600               1310
          7698         30       2850         1566.66667
    
    SQL>
    The following query shows one method for accessing data from previous and following rows within the current row using the windowing_clause. This can also be accomplished with LAG and LEAD.
    SELECT empno, deptno, sal, 
           FIRST_VALUE(sal) OVER (ORDER BY sal ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS previous_sal,
           LAST_VALUE(sal) OVER (ORDER BY sal ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS next_sal
    FROM   emp;
    
         EMPNO     DEPTNO        SAL PREVIOUS_SAL   NEXT_SAL
    ---------- ---------- ---------- ------------ ----------
          7369         20        800          800        950
          7900         30        950          800       1100
          7876         20       1100          950       1250
          7521         30       1250         1100       1250
          7654         30       1250         1250       1300
          7934         10       1300         1250       1500
          7844         30       1500         1300       1600
          7499         30       1600         1500       2450
          7782         10       2450         1600       2850
          7698         30       2850         2450       2975
          7566         20       2975         2850       3000
          7788         20       3000         2975       3000
          7902         20       3000         3000       5000
          7839         10       5000         3000       5000
    
    SQL>

    Using Analytic Functions

    The best way to understand what analytic functions are capable of is to play around with them. This article contains links to other articles I've written about specific analytic functions and the following documentation links list all analytic functions available in Oracle 11g Release 2. The "*" indicates that these functions allow for the full analytic syntax, including the windowing_clause.

    • AVG *
    • CORR *
    • COUNT *
    • COVAR_POP *
    • COVAR_SAMP *
    • CUME_DIST
    • DENSE_RANK
    • FIRST
    • FIRST_VALUE *
    • LAG
    • LAST
    • LAST_VALUE *
    • LEAD
    • LISTAGG
    • MAX *
    • MIN *
    • NTH_VALUE *
    • NTILE
    • PERCENT_RANK
    • PERCENTILE_CONT
    • PERCENTILE_DISC
    • RANK
    • RATIO_TO_REPORT
    • REGR_ (Linear Regression) Functions *
    • ROW_NUMBER
    • STDDEV *
    • STDDEV_POP *
    • STDDEV_SAMP *
    • SUM *
    • VAR_POP *
    • VAR_SAMP *
    • VARIANCE *