You are on page 1of 15

SNIPPET NAME: RANK

DESCRIPTION: RANK is an analytic function that returns the rank of a value in a group of values. It is very similar to
the dense_rank function. However, the rank function can cause non-consecutive rankings if the tested values are the
same. Whereas, the dense_rank function will always result in consecutive rankings.
Syntax #2 - Used as an Analytic Function
As an Analytic function, the rank returns the rank of each row of a query with respective to the other rows.
RANK(<rank expression>) WITHIN GROUP
(ORDER BY <expression> <ASC|DESC> NULLS <FIRST|LAST>)

-- Syntax #1 - Used as an Aggregate Function


-- As an Aggregate function, the rank returns the rank of a
-- row within a group of rows. The syntax for the rank function
-- when used as an Aggregate function is:
RANK( expression1, ... expression_n ) WITHIN GROUP
( ORDER BY expression1, ... expression_n )

-- expression1 .. expression_n can be one or more expressions


-- which identify a unique row in the group.
-- The SQL statement below would return the rank of an employee
-- with a salary of $1,000 and a bonus of $500 from within the
-- employees table.
SELECT RANK(1000, 500) WITHIN GROUP (ORDER BY salary, bonus)
FROM employees;

-- The syntax for the rank function when used as an Analytic


-- function is:
RANK() OVER ( [ query_partition_clause] ORDER BY clause )
SELECT employee_name, salary,
RANK() OVER (PARTITION BY department ORDER BY salary)
FROM employees
WHERE department = 'Marketing';
SNIPPET NAME: DENSE_RANK
DESCRIPTION: Ranks items in a group leaving no gaps in ranking sequence when there are ties.
DENSE_RANK() OVER (<query_partition_clause> <order_by_clause>)

conn oe/oe
SELECT d.department_name, e.last_name, e.salary, DENSE_RANK()
OVER (PARTITION BY e.department_id ORDER BY e.salary) AS DENSE_RANK
FROM employees e, departments d
WHERE e.department_id = d.department_id

AND d.department_id IN (30, 60);


SNIPPET NAME: FIRST
DESCRIPTION: Returns the row ranked first using DENSE_RANK
SELECT <aggregate_function(column_name)> KEEP
(DENSE_RANK FIRST ORDER BY <column_name> [<ASC|DESC> NULLS <FIRST|LAST>)
OVER (PARTITION BY <column_name>)
FROM <table_name>
GROUP BY <column_name>;

conn oe/oe
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
WHERE department_id IN (30, 60)
ORDER BY department_id, salary;
SNIPPET NAME: COVAR_POP
DESCRIPTION: COVAR_POP returns the population covariance of a set of number pairs. Both expr1 and expr2 are
numeric expressions. Virtuoso applies the function to the set of (expr1, expr2) pairs after eliminating all pairs for
which either expr1 or expr2 is null. Then Virtuoso makes the following computation:
(SUM(expr1 * expr2) - SUM(expr2) * SUM(expr1) / n) / n
where n is the number of (expr1, expr2) pairs where neither expr1 nor expr2 is null.
COVAR_POP(<expression1>, <expression2>) OVER (<analytic clause>)

conn oe/oe
SELECT project_id,
COVAR_POP(SYSDATE-hire_date, salary) AS COVAR_POP,
COVAR_SAMP(SYSDATE-hire_date, salary) AS COVAR_SAMP
FROM employee_list
WHERE department_id IN (50, 80)
GROUP BY project_id;
SNIPPET NAME: COVAR_SAMP
DESCRIPTION: COVAR_SAMP returns the sample covariance of a set of number pairs. Both expr1 and expr2 are
numeric expressions. Virtuoso applies the function to the set of (expr1, expr2) pairs after eliminating all pairs for
which either expr1 or expr2 is null. Then Virtuoso makes the following computation:
(SUM(expr1 * expr2) - SUM(expr2) * SUM(expr1) / n) / (n-1)
where n is the number of (expr1, expr2) pairs where neither expr1 nor expr2 is null.

COVAR_SAMP(<expression1>, <expression2>) OVER (<analytic clause>)

conn oe/oe
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;
SNIPPET NAME: CUME_DIST
DESCRIPTION: Returns the cumulative distribution of a value in a group of values.
CUME_DIST(<value>) OVER (<partition_clause> <order BY clause>)

conn oe/oe
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%';
SNIPPET NAME: FIRST_VALUE
DESCRIPTION: 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.
FIRST_VALUE(<expression> [IGNORE NULLS])

OVER (<analytic clause>)


conn oe/oe
SELECT last_name, salary, hire_date, FIRST_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);
SNIPPET NAME: Frequency Distribution
DESCRIPTION: Returns the frequency of a a given value in a set of values.
CREATE TABLE products (
prod1 NUMBER(3),
prod2 NUMBER(3),
prod3 NUMBER(3));
INSERT
INSERT
INSERT
INSERT
INSERT

INTO
INTO
INTO
INTO
INTO

products
products
products
products
products

VALUES
VALUES
VALUES
VALUES
VALUES

(34,23,45);
(34,22,34);
(54,44,45);
(23,22,45);
(45,22,34);

SELECT prod1, COUNT(prod1) OVER (PARTITION BY prod1) freq1,


prod2, COUNT(prod2) OVER (PARTITION BY prod2) freq2,
prod3, COUNT(prod3) OVER (PARTITION BY prod3) freq3
FROM products;

SNIPPET NAME: IGNORE NULLS


DESCRIPTION: Disregards the presence of NULL chars in the specified column.

(<column_name> IGNORE NULLS)


CREATE TABLE t1 (
row_num NUMBER(3),
col1 VARCHAR2(15),
col2 VARCHAR2(15));
INSERT INTO
INSERT INTO
INSERT INTO
INSERT INTO
INSERT INTO
INSERT INTO
INSERT INTO
INSERT INTO
INSERT INTO
INSERT INTO
COMMIT;

t1
t1
t1
t1
t1
t1
t1
t1
t1
t1

VALUES
VALUES
VALUES
VALUES
VALUES
VALUES
VALUES
VALUES
VALUES
VALUES

(6,
(1,
(2,
(3,
(4,
(5,
(6,
(7,
(8,
(9,

NULL, NULL);

'Category 1', 'Mango');


NULL, NULL);
NULL, NULL);
NULL, 'Banana');
NULL, NULL);
NULL, NULL);
'Category 2', 'Vanilla');
NULL, NULL);
'Category 3', 'Strawberry');

SELECT * FROM t1;


SELECT row_num,
LAST_VALUE(col1 IGNORE NULLS) OVER (ORDER BY row_num) col1,
LAST_VALUE(col2 IGNORE NULLS) OVER (ORDER BY row_num) col2
FROM t1
ORDER BY row_num;

SNIPPET NAME: LAG


DESCRIPTION: LAG 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.
LAG(<value expression>, <offset>, <default>)
OVER ([<query PARTITION clause>] <order_by_clause>)

conn oe/oe
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';

SNIPPET NAME: LAST


DESCRIPTION: Returns the row ranked last using DENSE_RANK.

<aggregate function> KEEP (DENSE_RANK LAST ORDER BY


(<expression> <ASC | DESC> NULLS <FIRST | LAST>)

SNIPPET NAME: LAST_VALUE


DESCRIPTION: Returns the last value in an ordered set of values. If the last value in the set is null, then the function
returns NULL unless you specify IGNORE NULLS. This setting is useful for data densification. If you specify IGNORE
NULLS, then LAST_VALUE returns the first non-null value in the set, or NULL if all values are null.
LAST_VALUE (<expression> IGNORE NULLS) OVER (<analytic clause>)

conn oe/oe
SELECT last_name, salary, hire_date, FIRST_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);

SNIPPET NAME: LEAD


DESCRIPTION: LEAD provides access to a row at a given physical offset beyond that position.
LEAD(<expression, offset, default>)

[(<query_partition_clause>)]
OVER (<order_by_clause>)
SELECT submit_date, num_votes,
LEAD(num_votes, 1, 0) OVER (ORDER BY submit_date) AS NEXT_VAL
FROM votes;
SNIPPET NAME: MAX
DESCRIPTION: Returns the maximum value by partition.
MAX (<DISTINCT | ALL> expression) OVER (<analytic clause>)

conn oe/oe
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;

SNIPPET NAME: MIN


DESCRIPTION: Returns the minimum value by partition.

MIN (<DISTINCT | ALL> expression) OVER (<analytic clause>)

conn oe/oe
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;

SNIPPET NAME: NTILE


DESCRIPTION: 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.
NTILE (<expression>) OVER ([query_partition_clause] <order BY clause>)

conn oe/oe
SELECT last_name, salary,
NTILE(4) OVER (ORDER BY salary DESC) AS quartile
FROM employees
WHERE department_id = 100;

SNIPPET NAME: OVER PARTITION BY


DESCRIPTION: This demo returns employees that are making above average salary in their respective department.
NTILE (<expression>) OVER ([query_partition_clause] <order BY clause>)

conn hr/hr
col ename format a30
col department_name format a20
SELECT * FROM (
SELECT e.ffirst_name || ' ' || e.last_name ENAME, d.department_name,
e.salary, TRUNC(e.salary - AVG(e.salary) OVER (PARTITION BY

e.department_id)) sal_dif
FROM employees e, departments d
WHERE e.department_id=d.department_id)
WHERE sal_dif > 0
ORDER BY 2,4 DESC;

SNIPPET NAME: PERCENTILE_CONT


DESCRIPTION: Inverse distribution function that assumes a continuous distribution model. It takes a percentile value
and a sort specification, and returns an interpolated value that would fall into that percentile value with respect to the
sort specification. Nulls are ignored in the calculation.

PERCENTILE_CONT(<value>) WITHIN GROUP (ORDER BY <expression> [ASC | DESC])

OVER (<partition_clause>)
conn oe/oe

SELECT last_name, salary, department_id,


PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY salary DESC)
OVER (PARTITION BY department_id) PCT_CONT, PERCENT_RANK()
OVER (PARTITION BY department_id ORDER BY salary DESC) PCT_RANK
FROM employees
WHERE department_id IN (30, 60);

SNIPPET NAME: PERCENTILE_DISC


DESCRIPTION: An inverse distribution function that assumes a discrete distribution model. It takes a percentile value
and a sort specification and returns an element from the set. Nulls are ignored in the calculation.
PERCENTILE_DISC(<expression>) WITHIN GROUP (ORDER BY <order_by_clause>)

conn oe/oe
col CUME_DIST format 9.999
SELECT last_name, salary, department_id,
PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY salary DESC)
OVER (PARTITION BY department_id) PCT_DISC,
CUME_DIST() OVER (PARTITION BY department_id
ORDER BY salary DESC) CUME_DIST
FROM employees
WHERE department_id IN (30, 60);

SNIPPET NAME: PERCENT_RANK


DESCRIPTION: 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).
PERCENT_RANK(<value>) OVER (<partition_clause> <order_by_clause>)

conn oe/oe
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;

SNIPPET NAME: REGR_AVGX


DESCRIPTION: REGR_AVGX evaluates the average of the independent variable (expr2) of the regression line. It
makes the following computation after the elimination of null (expr1, expr2) pairs:
AVG(expr2)

Generic Syntax
FUNCTION_NAME (<expression1>,<expression2>) OVER
(<analytic_clause>)
REGR_AVGX

conn oe/oe
SELECT job_id, employee_id ID, salary,
REGR_SLOPE(SYSDATE-hire_date, salary)
OVER (PARTITION BY job_id) slope,
REGR_INTERCEPT(SYSDATE-hire_date, salary)
OVER (PARTITION BY job_id) intcpt,
REGR_R2(SYSDATE-hire_date, salary)
OVER (PARTITION BY job_id) rsqr,
REGR_COUNT(SYSDATE-hire_date, salary)
OVER (PARTITION BY job_id) COUNT,
REGR_AVGX(SYSDATE-hire_date, salary)
OVER (PARTITION BY job_id) avgx,
REGR_AVGY(SYSDATE-hire_date, salary)
OVER (PARTITION BY job_id) avgy
FROM employees
WHERE department_id IN (50, 80)
ORDER BY job_id, employee_id;

SNIPPET NAME: REGR_AVGY


DESCRIPTION: REGR_AVGY evaluates the average of the independent variable (expr1) of the regression line. It
makes the following computation after the elimination of null (expr1, expr2) pairs:
AVG(expr1)

Generic Syntax
FUNCTION_NAME (<expression1>,<expression2>) OVER
(<analytic_clause>)
conn oe/oe
SELECT job_id,
REGR_AVGY(SYSDATE - hire_date, salary) avgy,
REGR_AVGX(SYSDATE - hire_date, salary) avgx
FROM employees
WHERE department_id IN (30, 50)
GROUP BY job_id;

SNIPPET NAME: REGR_COUNT


DESCRIPTION: REGR_COUNT Returns the number of non-null numbers used to fit the regression line.
Parameters
expr1 Number expression.
expr2 Number expression.

Generic Syntax: FUNCTION_NAME (<expression1>,<expression2>) OVER


(<analytic_clause>)

conn oe/oe
SELECT job_id,
REGR_COUNT(SYSDATE-hire_date, salary) COUNT
FROM employees
WHERE department_id IN (30, 50)
GROUP BY job_id;

SNIPPET NAME: REGR_INTERCEPT


DESCRIPTION: REGR_INTERCEPT returns the y-intercept of the regression line. After the elimination of null (expr1,
expr2) pairs, it makes the following computation:
AVG(expr1) - REGR_SLOPE(expr1, expr2) * AVG(expr2)

Generic Syntax: FUNCTION_NAME (<expression1>,<expression2>) OVER


(<analytic_clause>)
conn oe/oe
SELECT job_id,
REGR_SLOPE(SYSDATE - hire_date, salary) slope,
REGR_INTERCEPT(SYSDATE - hire_date, salary) intercept
FROM employees
WHERE department_id IN (50,80)
GROUP BY job_id
ORDER BY job_id;

SNIPPET NAME: REGR_R2


DESCRIPTION: REGR_R2 returns the coefficient of determination (also called "R-squared" or "goodness of fit") for
the regression. VAR_POP(expr1) and VAR_POP(expr2) are evaluated after the elimination of null pairs. The return
values are:
* NULL if VAR_POP(expr2) = 0
* 1 if VAR_POP(expr1) = 0 and VAR_POP(expr2) != 0
* (CORR(expr1,expr2) * CORR(expt1, expr2)) if VAR_POP(expr1) > 0 and VAR_POP(expr2) != 0

conn oe/oe
SELECT job_id, REGR_R2(SYSDATE-hire_date, salary) REGR_R2
FROM employees
WHERE department_id IN (50, 80)
GROUP BY job_id;

SNIPPET NAME: REGR_SLOPE


DESCRIPTION: REGR_SLOPE returns the slope of the line. After the elimination of null (expr1, expr2) pairs, it
makes the following computation:
COVAR_POP(expr1, expr2) / VAR_POP(expr2)

expr1 Number expression.


expr2 Number expression.
The function returns a value of type NUMERIC. If the function is applied to an empty set, then it returns null.
REGR_SLOPE

(IN expr1 ANY, IN expr2 ANY);

SELECT s_year,
REGR_SLOPE(s_amount, s_profit),
REGR_INTERCEPT(s_amount, s_profit)
FROM sales GROUP BY s_year;

/*
S_YEAR
REGR_SLOPE REGR_INTER
---------- ---------- ---------1998 128.401558 -2277.5684
1999 55.618655 226.855296
*/
SNIPPET NAME: REGR_SXX
DESCRIPTION: REGR_SXX makes the following computation after eliminating NULL (expr1, expr2) pairs:
REGR_COUNT(expr1, expr2) * VAR_POP(expr2)
Parameters
expr1 Number expression.
expr2 Number expression.

conn oe/oe
SELECT job_id,
REGR_SXY(SYSDATE - hire_date, salary) REGR_SXY,
REGR_SXX(SYSDATE - hire_date, salary) REGR_SXX,
REGR_SYY(SYSDATE - hire_date, salary) REGR_SYY
FROM employees
WHERE department_id IN (50, 80)
GROUP BY job_id
ORDER BY job_id;

SNIPPET NAME: REGR_SXY


DESCRIPTION: REGR_SXY makes the following computation after eliminating NULL (expr1, expr2) pairs:
REGR_COUNT(expr1, expr2) * COVAR_POP(expr1, expr2)
REGR_COUNT(expr1, expr2) * COVAR_POP(expr1, expr2)

SNIPPET NAME: REGR_SYY


DESCRIPTION: REGR_SYY makes the following computation after eliminating NULL (expr1, expr2) pairs:

REGR_COUNT(expr1, expr2) * VAR_POP(expr1)


Parameters
expr1 Number expression.
expr2 Number expression.
REGR_COUNT(expr1, expr2) * VAR_POP(expr1)

SNIPPET NAME: ROW_NUMBER


DESCRIPTION: Assigns a unique number to each row to which it is applied (either each row in the partition or each
row returned by the query), in the ordered sequence of rows specified in the order by clause, beginning with 1.
ROW_NUMBER(<value>) OVER (<partition_clause> ORDER BY <order_by_clause>)
CREATE TABLE test (
id
NUMBER(1),
degrees NUMBER(3));
INSERT INTO
INSERT INTO
INSERT INTO
INSERT INTO
INSERT INTO
INSERT INTO
INSERT INTO
INSERT INTO
INSERT INTO
INSERT INTO
INSERT INTO
INSERT INTO
INSERT INTO
INSERT INTO
COMMIT;

test
test
test
test
test
test
test
test
test
test
test
test
test
test

VALUES
VALUES
VALUES
VALUES
VALUES
VALUES
VALUES
VALUES
VALUES
VALUES
VALUES
VALUES
VALUES
VALUES

(0,235);
(0,276);
(1,211);
(1,250);
(1,255);
(2,55);
(2,277);
(2,69);
(3,25);
(3,166);
(3,262);
(4,47);
(4,238);
(4,40);

SELECT * FROM test;

-- choose the starting cell


SELECT id, degrees s
FROM (
SELECT id, degrees, (360 - degrees) d360,
ROW_NUMBER() OVER(PARTITION BY id
ORDER BY CASE
WHEN (degrees < 360 - degrees) THEN degrees
ELSE 360 - degrees
END) rn
FROM test) t
WHERE rn = 1;

-- order the rest clockwise


SELECT *
FROM (
SELECT t.id, t.degrees,
ROW_NUMBER() OVER(PARTITION BY t.id
ORDER BY CASE

WHEN (t.degrees < starting_cell.degrees) THEN t.degrees + 360


ELSE t.degrees
END) rn
FROM test t

JOIN (
SELECT id, degrees, (360 - degrees) d360,
ROW_NUMBER() OVER(PARTITION BY id
ORDER BY CASE
WHEN (degrees < 360 - degrees) THEN degrees
ELSE 360 - degrees
END) rn
FROM test) starting_cell
ON t.id = starting_cell.id
WHERE starting_cell.rn=1)t
ORDER BY id, rn;

SNIPPET NAME: STDDEV


DESCRIPTION: Returns the standard deviation. it returns STDDEV_SAMP if the number of pairs is more than one,
or NULL.
Parameters:
expr1 Number expression.
expr2 Number expression.
STDDEV([DISTINCT | ALL] <expression>) OVER (<analytic_clause>)

conn oe/oe
col STDDEV format 99999.999
SELECT last_name, salary,
STDDEV(salary) OVER (ORDER BY hire_date) "StdDev"
FROM employees
WHERE department_id = 30;

SNIPPET NAME: STDDEV_POP


DESCRIPTION: STDDEV_POP computes the population standard deviation and returns the square root of the
population variance. This function is the same as the square root of the VAR_POP function. When VAR_POP returns
null, this function returns null.
Parameters:
expr Number expression.
STDDEV_POP(<expression>) OVER (<analytic_clause>)

conn oe/oe
SELECT department_id, last_name, salary,
STDDEV_POP(salary) OVER (PARTITION BY department_id) AS pop_std
FROM employees;

SNIPPET NAME: STDDEV_SAMP


DESCRIPTION: STDDEV_SAMP computes the cumulative sample standard deviation and returns the square root of
the sample variance. The expr is a numeric expression, and the function returns a value of type NUMERIC. This
function is same as the square root of the VAR_SAMP function. When VAR_SAMP returns null, this function returns
null.
Parameters:
expr Number expression.

Computes the cumulative sample standard deviation AND returns the square root
OF the sample VARIANCE.
STDDEV_SAMP(<expression>) OVER
(<analytic_clause>)
conn oe/oe
SELECT department_id, last_name, hire_date, salary,
STDDEV_SAMP(salary) OVER (PARTITION BY department_id
ORDER BY hire_date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cum_sdev
FROM employees;
SNIPPET NAME: SUM
DESCRIPTION: The function returns the sum, for each node in every argument node-set, of the result of converting
the string-values of the node to a number. If some arguments are not node-sets, they are converted to numbers first.
Note that this definition differs from XPATH 1.0 standard, where sum() function must have exactly one argument of
type node-set. It is important that other XPATH processors may quietly ignore all arguments except the first one,
producing unexpected results.
Being called without arguments, sum() will return zero.
CREATE TABLE vote_count (
submit_date DATE NOT NULL,
num_votes
NUMBER NOT NULL);
INSERT INTO
INSERT INTO
INSERT INTO
INSERT INTO
INSERT INTO
COMMIT;

vote_count
vote_count
vote_count
vote_count
vote_count

VALUES
VALUES
VALUES
VALUES
VALUES

(TRUNC(SYSDATE)-4,
(TRUNC(SYSDATE)-3,
(TRUNC(SYSDATE)-2,
(TRUNC(SYSDATE)-3,
(TRUNC(SYSDATE)-1,

100);
150);
75);
25);
50);

SELECT * FROM vote_count;


SELECT submit_date, num_votes, SUM(num_votes)
OVER(ORDER BY submit_date ROWS UNBOUNDED PRECEDING) TOT_VOTE
FROM vote_count
ORDER BY submit_date;

SNIPPET NAME: VARIANCE


DESCRIPTION: VARIANCE returns the variance of expr. You can use it as an aggregate or analytic function.

Oracle Database calculates the variance of expr as follows:


*
0 if the number of rows in expr = 1
*
VAR_SAMP if the number of rows in expr > 1
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.
This function takes as an argument any numeric datatype or any nonnumeric datatype that can be implicitly
converted to a numeric datatype. The function returns the same datatype as the numeric datatype of the argument.
VARIANCE([DISTINCT | ALL] <value>) OVER (<analytic_clause>)

--Aggregate Example
--The following example calculates the variance of
-- all salaries in the sample employees table:
SELECT VARIANCE(salary) "Variance"
FROM employees;
VARIANCE

---------15283140.5
--Analytic Example
--The following example returns the cumulative variance of
-- salary values in Department 30 ordered by hire date.
SELECT last_name, salary, VARIANCE(salary)
OVER (ORDER BY hire_date) "Variance"
FROM employees
WHERE department_id = 30;

LAST_NAME
SALARY
VARIANCE
--------------- ---------- ---------Raphaely
11000
0
Khoo
3100
31205000
Tobias
2800 21623333.3
Baida
2900 16283333.3
Himuro
2600
13317000
Colmenares
2500
11307000

SNIPPET NAME: VAR_POP


DESCRIPTION: VAR_POP returns the population variance of a set of numbers after discarding the nulls in this set.
The expr is a number expression, and the function returns a value of type NUMERIC. If the function is applied to an
empty set, then it returns null. The function makes the following calculation:
(SUM(expr2) - SUM(expr)2 / COUNT(expr)) / COUNT(expr)

VAR_POP(<value>) OVER (<analytic_clause>)

conn sh/sh
SELECT t.calendar_month_desc, VAR_POP(SUM(s.amount_sold))
OVER (ORDER BY t.calendar_month_desc) "Var_Pop",
VAR_SAMP(SUM(s.amount_sold))
OVER (ORDER BY t.calendar_month_desc) "Var_Samp"
FROM sales s, times t
WHERE s.time_id = t.time_id AND t.calendar_year = 2001
GROUP BY t.calendar_month_desc;

SNIPPET NAME: VAR_SAMP


DESCRIPTION: VAR_SAMP returns the sample variance of a set of numbers after discarding the nulls in this set.
The expr is a numeric expression, and the function returns a value of type NUMERIC. If the function is applied to an
empty set, then it returns null. The function makes the following calculation:
(SUM(expr2) - SUM(expr)2 / COUNT(expr)) / (COUNT(expr) - 1)
This function is similar to VAR, except that given an input set of one element, VAR returns 0 and VAR_SAMP returns
null.
VAR_SAMP(<value>) OVER (<analytic_clause>)

You might also like