0% found this document useful (0 votes)
21 views

Analytical Function

Analytical functions allow querying data in groups and returning multiple rows per group, unlike aggregate functions which return a single value per group. Analytical functions include window functions like RANK(), DENSE_RANK(), and ROW_NUMBER() which assign sequential numbers to rows within partitions defined by a PARTITION BY clause ordered by a column in an ORDER BY clause. Common table expressions (WITH clauses) allow defining temporary tables to break queries into multiple logical parts and select from the results of analytical functions applied to those temporary tables.

Uploaded by

spider14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Analytical Function

Analytical functions allow querying data in groups and returning multiple rows per group, unlike aggregate functions which return a single value per group. Analytical functions include window functions like RANK(), DENSE_RANK(), and ROW_NUMBER() which assign sequential numbers to rows within partitions defined by a PARTITION BY clause ordered by a column in an ORDER BY clause. Common table expressions (WITH clauses) allow defining temporary tables to break queries into multiple logical parts and select from the results of analytical functions applied to those temporary tables.

Uploaded by

spider14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

ANALYTICAL FUNCTION:

(i).Analytical functions, also known as window functions.


(ii).Analytical functions calculate an aggregate value based on a group of rows and
return multiple rows for each group.

Aggregate Function or Group Function:


Group function process the values of multiple rows to return one result per group.

Analytical Function:
It process the vaues of multiple rows to return multiple rows for each set of
values.

Multiple rows it return one row --Aggregate


Each rows it return one row. --Analytical

select department_id, MAX(salary) from employees GROUP BY department_id;


select department_id, MAX(salary) OVER() FROM employees;
PARTITION BY,ORDER BY --optional clause
select department_id, MAX(salary) OVER(PARTITION BY department_id ORDER BY salary
desc) FROM employees;

with A as
(select employee_id,first_name,department_id,salary, ROW_NUMBER() OVER(PARTITION BY
department_id ORDER BY salary Desc) RNK from employees
) select * from A where RNK=1;

RANK
DENSE_RANK
ROW_NUMBER
LEAD
LAG
FIRST_VALUE
LAST_VALUE
CUBE
ROLLUP

RANK()
(i). It accept one arg.
(ii). It is used to provide rank for a column value.
(iii). If two records are same then it give same rank for both records, and next
records will be (n+2) rank.

SYNTAX:
RANK() OVER(PARTITION BY CLAUSE ORDER BY CLAUSE)

ie: 1
1
3

select Employee_id,First_name,salary, RANK() OVER(ORDER BY SALARY DESC) RNK from


employees;

select employee_id,first_name,department_id,salary, RANK() OVER(PARTITION BY


department_id ORDER BY salary Desc) RNK from employees;

select employee_id,first_name,department_id,salary, RANK() OVER(PARTITION BY


department_id ORDER BY SALARY DESC) RNK from employees where RNK=1; --RNK: invalid
identifier
Oracle Execution Order Query order

FROM Select
WHERE From
GROUP BY Where
HAVING Group by
SELECT Having
ORDER BY Order by

WITH clause(Common Table Expression (CTE)): is a named subquery(or temp table)


that you can reference within your main SQL query to make your code more organized
and easier to understand.

WITH A AS
(select Employee_id,First_name,salary, RANK() OVER(ORDER BY SALARY DESC) RNK from
employees)
select * from A where RNK=14;

WITH A AS
(select Employee_id,First_name,salary, RANK() OVER(ORDER BY SALARY DESC) RNK from
employees)
select * from A where RNK between 14 and 20;

WITH A AS
(select Employee_id,First_name,salary, RANK() OVER(ORDER BY SALARY DESC) RNK from
employees where salary between 10000 and 15000)
select * from A;

DENSE_RANK()

(i). It accept one arg.


(ii). It is used to provide rank for a column value.
(iii). If two records are same then it give same rank for both records, and next
records will be (n+1) rank.
(If multiple rows have the same values and are assigned the same rank, the next
rank will be consecutive, without any gaps)

ie: 1
1
2

SYNTAX:
DENSE_RANK() OVER(PARTITION BY CLAUSE ORDER BY CLAUSE)

select Employee_id,First_name,salary,
DENSE_RANK() OVER(ORDER BY SALARY DESC) RNK from employees;

select employee_id,first_name,department_id,salary, DENSE_RANK() OVER(PARTITION BY


department_id ORDER BY salary Desc) RNK from employees

WITH B AS
(select employee_id,first_name,department_id,salary, DENSE_RANK() OVER(PARTITION BY
department_id ORDER BY salary Desc) RNK from employees)
select * from B where RNK between 3 and 6;

ROW_NUMBER()

select Employee_id,First_name,salary, ROW_NUMBER() OVER(ORDER BY SALARY DESC) RNK


from employees;

with B as
(select Employee_id,First_name,salary, ROW_NUMBER() OVER(ORDER BY SALARY DESC) RNK
from employees)
select * from B where RNK between 90 and 95;

SELECT RNK, e.*


FROM (
SELECT e.*, RANK() OVER(PARTITION BY department_id ORDER BY SALARY DESC) RNK
FROM employees e
) ranked_employees;

You might also like