Analytical Function
Analytical Function
Analytical Function:
It process the vaues of multiple rows to return multiple rows for each set of
values.
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
FROM Select
WHERE From
GROUP BY Where
HAVING Group by
SELECT Having
ORDER BY Order by
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()
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;
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()
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;
LEAD()
SYNTAX:
LEAD(expr, offset, default) OVER(PARTITION BY CLAUSE ORDER BY CLAUSE)
LEAD()
LEAD(expr, offset, default) OVER(PARTITION BY CLAUSE ORDER BY CLAUSE)
SYNTAX:
INSERT ALL
INTO T_SALES VALUES('10-JAN-2021',100)
INTO T_SALES VALUES('15-FEB-2021',50)
INTO T_SALES VALUES('28-APR-2021',150)
INTO T_SALES VALUES('14-MAY-2021',200)
INTO T_SALES VALUES('05-SEP-2021',100)
INTO T_SALES VALUES('14-NOV-2021',70)
INTO T_SALES VALUES('10-DEC-2021',130)
SELECT * FROM DUAL;