Advanced SQL functions
-
Upload
surya-reddy -
Category
Documents
-
view
273 -
download
0
Transcript of Advanced SQL functions
-
7/28/2019 Advanced SQL functions
1/12
Analytic Functions
There are three types of SQL extensions that fall under the banner of "analytic functions" thoughthe first could be said to provide "analytic functionality" rather than actually be analytic
functions:
new grouping of resultsets through extensions to the GROUP BY clause (ROLLUP andCUBE);
the new analytic functions themselves; and TOP-N analysis (largely enabled by the analytic functions).
grouping extensions (rollup and cube)
Example One: Sums the salaries by job and then sub-totals each department (similar to a break sum
report in SQL*Plus) and then the entire salary column
select deptno,job,sum(sal)from emp groupbyrollup(deptno,job)
Example Two: Sums the salaries by job and then sub-totals each department and each job type
(similar to a break sum report in SQL*Plus) and then the entire salary column. This will providesub-totals for all columns within the GROUP BY clause.
select deptno,job,sum(sal)from emp groupbycube(deptno,job)
Example Three: Using the GROUPING function to "label" the sub-total rows (i.e. determinewhich rows represented rollups). The GROUPING function returns a value of 1 if the current
row is a row representing an aggregated rollup group (such as the sub-total rows) or zero if the
row is one of the "source" records itself.
selectdecode(grouping(deptno),1,'all depatments',deptno)as deptno,
decode(grouping(job),1,'job total',job)asjob,sum(sal)
from empgroupbycube(deptno,job)
selectdecode(grouping(deptno),1,'all depatments',deptno)as deptno,decode(grouping(job),1,'job total',job)asjob,sum(sal)
from empgroupbyrollup(deptno,job) ----- no use of grouping here
-
7/28/2019 Advanced SQL functions
2/12
analytic functions
There are 33 new analytic functions, though it is likely that most users will concentrate on a
small number of these and very few will use the statistical capabilities. They are:
AVG, MIN, MAX, COUNT, SUM LAG, LEAD FIRST_VALUE, LAST_VALUE ROW_NUMBER RANK, DENSE_RANK, PERCENT_RANK NTILE, CUME_DIST, RATIO_TO_REPORT CORR COVAR_POP VARIANCE, VAR_POP, VAR_SAMP STTDEV, STTDEV_POP, STDDEV_SAMP REGR_COUNT, REGR_P2, REGR_AVGX, REGR_AVGY, REGR_SXX,
REGR_SXY, REGR_SYY, REGR_SLOPE, REGR_INTERCEPT
The way analytic functions work is to manipulate data contained within returning resultsets. This
means they can process, merge and compute against data that has already been fetched from a query
and partition and order the resultset into groups while at the same time returning the entire resultset
without GROUP BY clauses.
Example Four: Calculate a running salary total for each department as new employees were
hired.
select ename,
deptno,sal,sum(sal)over(partitionby deptno orderby ename nullslast) dept_running_total,row_number()over(partitionby deptno orderby ename ascnullslast) empno_running_total
from emp
select ename,deptno,sal,sum(sal)over(partitionby deptno orderby ename ) running_total
from emp /* it will give the same result as above one. but if we have same sal as sequenced then itwill return wrong return_total */
select ename,
deptno,sal,sum(sal)over(partitionby deptno) running_total
from emp /* it will give dept wise total salaries , for every dept */
select ename,deptno,sal,
-
7/28/2019 Advanced SQL functions
3/12
sum(sal)over() running_total
from emp /* it will give the total salary of table for every record */
Analytic functions are invoked using the OVER() clause. This also enables Oracle to distinguish
between PL/SQL functions and analytic functions that share the same name such as AVG, MINand MAX.
There are three components to the OVER clause:
PARTITION clause, by which the resultset can be broken into groups, such asdepartments in the example above. Without this the entire resultset is treated as a singlepartition;
ORDER BY clause, by which the resultset or partition group can be ordered. This isoptional for some analytic functions but mandatory for those which need to access rows
either side of the current row, such as LAG and LEAD; and
RANGE or ROWS clause (AKA windowing), by which the function can be made toinclude rows or values around the current row in its calculations. RANGE windows work
on values and ROWS windows work on records, such as either X rows on each side ofthe current row or all rows preceding the current row, within the current partition.
The PARTITION and ORDER BY clauses are demonstrated in the first example above. Theresultset was partitioned into the individual departments in the organization. Within each
department, the data was ordered by ename (using default criteria (ASC and NULLS LAST). No
RANGE clause was added which means that we used the default of RANGE UNBOUNDED
PRECEDING, which means include all the preceding records in the current partition in thecalculation for the current row. The easiest way to understand analytic functions and windowing
is by examples which demonstrate the each of the three components to the OVER() clause.
Example Five: Find the average salary by department and compare each employees' salaries to
the department average.
SELECT deptno, ename, sal, ROUND(average_sal_dept,0)AS average_sal_dept
, ROUND(sal - average_sal_dept,0)AS sal_varianceFROM (SELECT deptno
, ename, sal
, AVG(sal)OVER(PARTITIONBYdeptno)AS average_sal_dept
FROM emp);
.-OR-.
select deptno,ename,sal,round((selectavg(SAl)from emp where deptno=e.deptno groupby deptno ))avg_sal ,sal-(round((selectavg(SAl)from emp where deptno=e.deptno groupby deptno )))sal_variance
-
7/28/2019 Advanced SQL functions
4/12
from emp e
groupby deptno,ename,salorderby deptno
Example Six: Determine the order by which employees joined their respective departments.
Also include the employees who preceded and succeeded them.
==> LAG() and LEAD() provide access to rows around the current row,
SELECT deptno,ename,hiredate,LAG (ename,1,NULL)
OVER(PARTITIONBYdeptno ORDERBYhiredateASCNULLSLAST)AS previous_employee_ename,
LEAD (ename,1,NULL)
OVER(PARTITIONBYdeptno ORDERBYhiredateASCNULLSLAST)
AS next_employee_enameFROM emp
ORDERBYdeptno;
Example Seven: Determine the proportion of each department's salary taken up by its individualemployees:
SELECT deptno,
ename,sal,dept_sal,
ROUND(employees_dept_ratio * 100,2)AS emps_proportionFROM(SELECT deptno,
-
7/28/2019 Advanced SQL functions
5/12
ename,
sal,SUM(sal)OVER(PARTITIONBYdeptno)AS dept_sal,
RATIO_TO_REPORT (sal)OVER(PARTITIONBYdeptno)AS employees_dept_ratio
FROM emp)
ORDERBYdeptno;
RANGE windowing:
Example Eight: Determine the first and last employee to be employed within 50 days of the current
employees' hiredate
select deptno,ename,
hiredate,first_value(ename)over() first_ename,last_value(ename)over() last_ename
from emp ----it will print first ename for all records, and last name for all records asfirst_ename and last_ename
select deptno,ename,
hiredate,first_value(ename)over(orderby hiredate rangebetween50precedingand50following)
first_emp,last_value(ename)over(orderby hiredate rangebetween50precedingand50following) last_emp
from emp --is the right solution
-
7/28/2019 Advanced SQL functions
6/12
ROWS windowing:
Example Nine: Determine who was recruited two employees before and three after the current
employee (note what happens when employees share hiredates).
select deptno,ename,hiredate,first_value(ename)over(orderby hiredate rows2preceding) two_emps_back,last_value(ename)over(orderby hiredate rows3preceding) three_emps_forward
from emp ---- here no use of last value whenever we are using preceding
select deptno,ename,hiredate,first_value(ename)over(orderby hiredate rows2preceding) two_emps_back,first_value(ename)over(orderby hiredate rows3preceding) three_emps_back
from emp
select deptno,
ename,hiredate,first_value(ename)over(orderby hiredate rows2preceding) two_emps_back,
first_value(ename)over(orderby hiredate descrows3preceding) three_emps_forwardfrom emp ---is the right solution
top-n queries
Example Ten: Who were the first three recruits to our organization?
SELECTROWNUMASrank
, ename, hiredate
FROM (SELECT ename, hiredateFROM empORDER BY
hiredateASCNULLSLAST)
WHERE ROWNUM
-
7/28/2019 Advanced SQL functions
7/12
answered? The ORDER BY in-line view method would generate the employees in no particular
order and the stopkey would stop returning rows at record three.
To remove this ambiguity, analytic functions can help. For the following example, I've updated
five employees to have the earliest date.
Example Eleven: Who were the first three recruits to our organization?
SELECT hire_rank, ename, hiredate
FROM (SELECT ename, hiredate
, RANK()OVER(ORDERBYHIREDATEASCNULLSLAST)AS hire_rank
FROM emp)
WHERE hire_rank
-
7/28/2019 Advanced SQL functions
8/12
(ORDER BY sal DESC NULLS LAST) AS company_ranking
FROM emp
ORDER BYdeptno;
DEPTNO ENAME SAL DEPT_RANKING COMPANY_RANKING---------- ---------- ---------- ------------ ---------------
10 KING 5000 1 110 CLARK 2450 2 5
10 MILLER 1300 3 820 SCOTT 3000 1 220 FORD 3000 1 2
20 JONES 2975 2 3
20 ADAMS 1100 3 10
20 SMITH 800 4 1230 BLAKE 2850 1 430 ALLEN 1600 2 6
30 TURNER 1500 3 730 WARD 1250 4 9
30 MARTIN 1250 4 9
30 JAMES 950 5 11
Note: The use of DENSE_RANK() and opposed to RANK() means that no rank numbers areskipped. For example, in department 20, SCOTT and FORD have the same salary so they share a
dense_rank of 1, while JONES (next highest) has the dense_rank of 2. With RANK(), JONES
would be ranked 3, as rank is relative to the number of rows, so the RANK() for SCOTT, FORD
and JONES would be 1,1,3 respectively.
-
7/28/2019 Advanced SQL functions
9/12
==::Hierarchical Quaries::==
selectlevel,empno,ename,mgrfrom empstartwith empno=7902
connectbyprior empno=mgr
selectlevel,empno,ename,mgrfrom empstartwith mgr isnull
connectbyprior empno=mgr
select ename ||'reports to'||prior ename "Reporting Details of employee"
from empstartwith mgr isnullconnectbyprior empno=mgr
-
7/28/2019 Advanced SQL functions
10/12
Use Of Purge, Flash Back and Recyclebin :
select * from emp
createtable sunsys asselect * from empo/p: Table created
select * from sunsys
-
7/28/2019 Advanced SQL functions
11/12
droptable sunsyso/p:table dropped
select * from sunsys
Recyclebin: is used to store all objects,which were dropped.
select * fromrecyclebinwhereoriginal_name='SUNSYS'
Purge : is used to delete tables which are in recyclebin
purgetable sunsysselect * fromrecyclebinwhere original_name='SUNSYS'
o/p: No Rows Returned
-
7/28/2019 Advanced SQL functions
12/12
OR
Flashback: is used to get back the table,which has been dropped
flashbacktable sunsys tobeforedropselect * from sunsys