0% found this document useful (0 votes)
108 views24 pages

Analaytical Function-Pravin

Analytical functions allow computations to be performed on groups of rows and return a result for each row in the query. They differ from aggregate functions in that they do not reduce the number of rows returned. Common analytical functions include rank, dense_rank, row_number, lag, lead, first_value and last_value. These functions can be used to rank or assign sequence numbers to rows, access data from previous or following rows, and return the first or last value within a partition. Analytical functions allow insights to be gained from relationships within the data that are not possible with normal aggregation.

Uploaded by

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

Analaytical Function-Pravin

Analytical functions allow computations to be performed on groups of rows and return a result for each row in the query. They differ from aggregate functions in that they do not reduce the number of rows returned. Common analytical functions include rank, dense_rank, row_number, lag, lead, first_value and last_value. These functions can be used to rank or assign sequence numbers to rows, access data from previous or following rows, and return the first or last value within a partition. Analytical functions allow insights to be gained from relationships within the data that are not possible with normal aggregation.

Uploaded by

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

PRESENTED BY

PRAVIN R
 Analytical Functions or Windowing Functions or OLAP
(online analytical processing)function were introduced in
Oracle8i.
 They compute aggregate values based on groups of data.
 Probably the easiest way to understand analytical 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 VIN
table.
SELECT AVG(sal)
FROM vin;
 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, round(AVG(sal),1)


FROM vin
GROUP BY deptno
ORDER BY deptno;

Group by function work on the


multiple rows and return single row as
output.
 In both cases, the aggregate function reduces the number of
rows returned by the query.
 Analytical 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 vin;

Analaytical function works on the


multiple rows and return result for
each row .
 Rank
 Dense rank
 First value
 Last value
 Lag
 Lead
 Row number
 Count
 Listagg()
 Wm.concate()
SELECT empno, deptno, sal,
rank() OVER (PARTITION BY deptno order by sal) AS
my_rank
FROM vin;
What we see here is where two people have the same salary
they are assigned the same rank. The rank function skip the 5
rank and display 6th rank for next salary is greater than 3000 in
department_id=20
SELECT empno, deptno, sal,
dense_rank() OVER (PARTITION BY deptno order by sal) AS
my_rank
FROM vin;
What we see here is where two people have the same salary
they are assigned the same rank. The dense_rank function
won’t skip the rank and display 3th rank for next salary is
greater than 1250 in department_id=30
The first_value function is used to return the first value from an
order sequence.

SELECT empno, deptno, sal,


FIRST_VALUE(sal) OVER (PARTITION BY deptno) AS
first_sal_in_dept FROM vin;
The first_value function is used to return the last value
from an order sequence.

SELECT empno, deptno, sal,


last_VALUE(sal) OVER (PARTITION BY deptno)
AS first_sal_in_dept FROM vin;
The LAG function is used to access data from a previous row.
The following query returns the salary from the previous row
to calculate the difference between the salary of the current
row and that of the previous row. Notice that the ORDER
BY of the LAG function is used to order the data by salary.

SELECT empno, ename, job, sal,


LAG(sal, 1, 0) OVER (ORDER BY sal) AS sal_prev
FROM vin;
 The LEAD function is used to return data from rows further
down the result set. The following query returns the salary
from the next row to calculate the difference between the
salary of the current row and the following row.
 SELECT empno, ename, job, sal, LEAD(sal, 2, 99) OVER
(ORDER BY sal) AS sal_next FROM vin;
 It is used to assign a unique number from 1-N to the rows with
in a partition.at first glance this may seem similar to the rank
function and dense_rank function ignores ties and always
gives an unique number to each row
SELECT empno,ename,deptno,sal,
ROW_NUMBER() OVER (ORDER BY sal) AS row_num,
RANK() OVER (ORDER BY sal) AS row_rank,
DENSE_RANK() OVER (ORDER BY sal) AS row_dense_rank
FROM vin;
SELECT *
FROM (SELECT empno, ename,deptno,sal,
ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY sal DESC)
AS row_num
FROM vin)
WHERE row_num = 2;
Omitting a partitioning clause from the OVER clause means
the whole result set is treated as a single partition. In the
following example we display the number of employees, as
well as all the original data.
SELECT deptno,sal,
Adding the partitioning clause allows
COUNT(*) OVER () AS cou_nt
us to return the count within a
FROM vin; partition.

SELECT deptno,sal,
COUNT(*) OVER (PARTITION BY deptno)
AS amount_by_dept
FROM vin;
The LISTAGG analytic function was introduced in Oracle 11g
Release 2, making it very easy to aggregate strings. The nice
thing about this function is it also allows us to order the
elements in the concatenated list. If you are using 11g Release
2 you should use this function for string aggregation.

SELECT deptno, LISTAGG(ename, ',')WITHIN GROUP (ORDER BY


ename) AS employees
FROM vin
GROUP BY deptno;
If you are not running 11g Release 2 or above, but are running
a version of the database where the WM_CONCAT function is
present, then it is a zero effort solution as it performs the
aggregation for you
SELECT deptno, wm_concat(ename) AS employees
FROM vin
GROUP BY deptno;
Find the 5 th max salary of employee using subquery.
select salary
from (select salary from (select distinct salary from employees order by salary desc)
where rownum<6
order by salary) where rownum=1;

select * from table (dbms_xplan.display);


Find the 5 th max salary of employee using analaytical function.

select salary from (select dense_rank() over(order by salary


desc) den_se,
e.* from employees e) where den_se=&n;

select * from table (dbms_xplan.display);


 SELECT * FROM EMPLOYEES
 WHERE ROWNUM < 5;
 /
 SELECT EMPLOYEE_ID,
 DEPARTMENT_ID,
 SALARY,
 RANK() OVER (PARTITION BY DEPARTMENT_ID ORDER BY SALARY DESC) AS MY_RANK
 FROM EMPLOYEES;
 /
 SELECT EMPLOYEE_ID,
 DEPARTMENT_ID,
 SALARY,
 dense_RANK() OVER (PARTITION BY DEPARTMENT_ID ORDER BY SALARY DESC) AS MY_RANK
 FROM EMPLOYEES;
 /
 SELECT * FROM
 (
 SELECT EMPLOYEE_ID,
 DEPARTMENT_ID,
 SALARY,
 DENSE_RANK() OVER (PARTITION BY DEPARTMENT_ID ORDER BY SALARY DESC) AS MY_RANK
 FROM EMPLOYEES)
 where my_rank = 1;
 /
 /
 SELECT EMPLOYEE_ID,
 DEPARTMENT_ID,
 SALARY,
 row_number() OVER (PARTITION BY DEPARTMENT_ID ORDER BY SALARY DESC) AS MY_RANK
 FROM EMPLOYEES;
 /
 SELECT EMPLOYEE_ID,
 DEPARTMENT_ID,
 SALARY,
 ROW_NUMBER() OVER (ORDER BY SALARY DESC) AS MY_RANK
 FROM EMPLOYEES;
 /
 SELECT EMPLOYEE_ID,
 DEPARTMENT_ID,
 SALARY,
 FIRST_VALUE(SALARY) OVER (PARTITION BY DEPARTMENT_ID) AS FIRST_SAL_IN_DEPT
 FROM EMPLOYEES;
 /
 SELECT EMPLOYEE_ID,
 DEPARTMENT_ID,
 SALARY,
 FIRST_VALUE(SALARY) OVER (order by salary desc) AS FIRST_SAL_IN_DEPT
 FROM EMPLOYEES;
 /
 SELECT EMPLOYEE_ID,
 DEPARTMENT_ID,
 SALARY,
 LAST_VALUE(SALARY) OVER (PARTITION BY DEPARTMENT_ID) AS FIRST_SAL_IN_DEPT
 FROM EMPLOYEES;
 /
 /
 SELECT EMPLOYEE_ID,
 DEPARTMENT_ID,
 SALARY,
 LAST_VALUE(SALARY) OVER (ORDER BY SALARY DESC) AS FIRST_SAL_IN_DEPT
 FROM EMPLOYEES;
 /
 SELECT EMPLOYEE_ID,
 DEPARTMENT_ID,
 JOB_ID,
 SALARY,
 LAG(SALARY, 1, 0) OVER (ORDER BY SALARY) AS SAL_PREV
 FROM EMPLOYEES;
 /
 SELECT EMPLOYEE_ID,
 DEPARTMENT_ID,
 JOB_ID,
 SALARY,
 LAG(SALARY, 1, 0) OVER (PARTITION BY DEPARTMENT_ID ORDER BY SALARY) AS SAL_PREV
 FROM EMPLOYEES;
 /
 SELECT EMPLOYEE_ID,
 DEPARTMENT_ID,
 JOB_ID,
 SALARY,
 LEAD(SALARY, 1, 0) OVER (ORDER BY SALARY) AS SAL_NEXT
 FROM EMPLOYEES;
 /
 /
 SELECT EMPLOYEE_ID,
 DEPARTMENT_ID,
 JOB_ID,
 SALARY,
 LEAD(SALARY, 1, 0) OVER (PARTITION BY DEPARTMENT_ID ORDER BY SALARY) AS
SAL_NEXT
 FROM EMPLOYEES;
 /
 SELECT DEPARTMENT_ID,
 LISTAGG(FIRST_NAME, ',') WITHIN GROUP (ORDER BY FIRST_NAME) AS EMPL
 FROM EMPLOYEES
 GROUP BY DEPARTMENT_ID;
 /
 SELECT DEPARTMENT_ID,
 WM_CONCAT(FIRST_NAME) AS empl
 FROM employees
 GROUP BY DEPARTMENT_ID;
 /
 SELECT salary
 FROM ( SELECT salary FROM
 (SELECT DISTINCT salary FROM employees ORDER BY salary DESC )
 WHERE rownum<6
 ORDER BY SALARY )
 WHERE ROWNUM=1;
THANK YOU...

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy