Professional Documents
Culture Documents
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>)
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
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.
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])
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);
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);
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';
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);
[(<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;
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;
conn oe/oe
SELECT last_name, salary,
NTILE(4) OVER (ORDER BY salary DESC) AS quartile
FROM employees
WHERE department_id = 100;
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;
OVER (<partition_clause>)
conn oe/oe
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);
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;
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;
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;
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;
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;
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;
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);
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;
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;
conn oe/oe
SELECT department_id, last_name, salary,
STDDEV_POP(salary) OVER (PARTITION BY department_id) AS pop_std
FROM employees;
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);
--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
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;