analytical function

2
Analytic Functions Analytic functions compute an aggregate value based on a group of rows. They dif fer from aggregate functions in that they return multiple rows for each group. Analytic functions are the last set of operations performed in a query except fo r the final ORDER BY clause. All joins and all WHERE, GROUP BY, and HAVING claus es are completed before the analytic functions are processed. Therefore, analyti c functions can appear only in the select list or ORDER BY clause. Analytic functions Syntax :- analytic_function(argu ments) over (query_partition_by Order_by_clause) Use OVER analytic_clause to indicate that the function operates on a query resul t set. That is, it is computed after the FROM, WHERE, GROUP BY, and HAVING claus es. You can specify analytic functions with this clause in the select list or ORDER BY clause. To filter the results of a query based on an analytic function, nest these funct ions within the parent query, and then filter the results of the nested sub-quer y. query_partition_by Use the PARTITION BY clause to partition the query result set into groups based on one or more value_expr. If you omit this clause, then the function treats all rows of the query result set as a single group. example : sum(sal) over ( partition by deptno,mgrno) Order_by_clause Use the order_by_clause to specify how data is ordered within a partition. ASC | DESC Specify the ordering sequence (ascending or descending). ASC is the d efault. NULLS FIRST | NULLS LAST Specify whether returned rows containing nulls should a ppear first or last in the ordering sequence. NULLS LAST is the default for ascending order, and NULLS FIRST is the default fo r descending order. example : sum(sal) over ( partition by deptno,mgrno order by empno DESC NULLS FI RST ) Analytic functions always operate on rows in the order specified in the order_by  _clause of the functio n. However, the order_by _clause of the function d oes not g uarantee the order of the result. Use the order_by_clause of the query to guaran tee the final result ordering. analytic_function AVG COUNT FIRST FIRST_VALUE LAG LAST LAST_VALUE

Upload: ravi-kumar-pasupula

Post on 14-Apr-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

7/30/2019 Analytical Function

http://slidepdf.com/reader/full/analytical-function 1/2

Analytic FunctionsAnalytic functions compute an aggregate value based on a group of rows. They differ from aggregate functions in that they return multiple rows for each group.

Analytic functions are the last set of operations performed in a query except for the final ORDER BY clause. All joins and all WHERE, GROUP BY, and HAVING clauses are completed before the analytic functions are processed. Therefore, analytic functions can appear only in the select list or ORDER BY clause.

Analytic functions Syntax :-

analytic_function(arguments) over (query_partition_by Order_by_clause)

Use OVER analytic_clause to indicate that the function operates on a query result set. That is, it is computed after the FROM, WHERE, GROUP BY, and HAVING clauses.You can specify analytic functions with this clause in the select list or ORDERBY clause.To filter the results of a query based on an analytic function, nest these functions within the parent query, and then filter the results of the nested sub-query.

query_partition_by

Use the PARTITION BY clause to partition the query result set into groups basedon one or more value_expr. If you omit this clause, then the function treats allrows of the query result set as a single group.

example : sum(sal) over ( partition by deptno,mgrno)

Order_by_clause

Use the order_by_clause to specify how data is ordered within a partition.ASC | DESC Specify the ordering sequence (ascending or descending). ASC is the d

efault.NULLS FIRST | NULLS LAST Specify whether returned rows containing nulls should appear first or last in the ordering sequence.NULLS LAST is the default for ascending order, and NULLS FIRST is the default for descending order.

example : sum(sal) over ( partition by deptno,mgrno order by empno DESC NULLS FIRST )

Analytic functions always operate on rows in the order specified in the order_by _clause of the function. However, the order_by_clause of the function does not guarantee the order of the result. Use the order_by_clause of the query to guarantee the final result ordering.

analytic_function

AVGCOUNTFIRSTFIRST_VALUELAGLASTLAST_VALUE

7/30/2019 Analytical Function

http://slidepdf.com/reader/full/analytical-function 2/2

LEADMAXMINRANKROW_NUMBERSUMVARIANCEI will clarify later using of that functions in part 2.

HintFor IT people who use Microsoft SQL Server, It is the same syntax.Unfortunately Mysql doesn't support partition by clause.

/********************************************************************************/*/* The following is comment on challenge indicated to theprevioud topic /*/********************************************************************************/I created challenge at my Facebook wall that indicated to that topic.Challenge QuestionI want select statement that show employees and his serial in his departmentlike the followingEmpNO ENAME DeptNO Serial

7782 "CLARK" 10 17839 "KING" 10 27369 "SMITH" 20 17566 "JONES" 20 27499 "ALLEN" 30 17521 "WARD" 30 2Challenge Solution1- First solution that uses partition by clause (Oracle RDBMS and Microsoft SQLServer)SELECT empno, ename, deptno,

COUNT (deptno) OVER (PARTITION BY deptno ORDER BY empno)FROM scott.emp

2- Second solution that uses left outer join and that solution have bad performance. ( SQL Standard )SELECT a.empno, a.ename, a.deptno, COUNT (*) serial

FROM scott.emp a LEFT OUTER JOIN scott.emp bON a.deptno = b.deptno AND a.empno >= b.empno

GROUP BY a.empno, a.ename, a.deptnoORDER BY a.deptno, a.empno

3- Third solution that can be executed at Mysql onlyset @deptartment = 0;set @serial = 1;

select empno, ename, deptno

@serial := if(@deptartment = deptno, @serial + 1, 1) as row_number,@deptartment := deptno as dummy

from scott.emporder by deptno,empno;