You are on page 1of 12

Total Online Users: 1896

Home | Forums | Videos | Blogs Login


E nter U s erI D or E mail

Submit an Article Go Advanced Search

Search :

Home Oracle Aggregate Function in Oracle

Remember Me Sign In Register

Aggregate Function in Oracle


By Sapna Malik November 02, 2010
In this article, we will learn aggregate function in Oracle.

Author Rank: Total page views : 526 Total downloads : 0 Share

Forgot Password Forgot Username Why Register Jump to Technology Website Sponsored by Become a Sponsor Resources About Us Authors News & Events Product Reviews Our Network .NET Heaven C# Corner Interview Corner Longhorn Corner VB.NET Heaven

Print Email to a friend

Post a comment Bookmark

Similar Articles Author's other articles Sponsored by

Aggregate Function
Similar Articles

Become a Sponsor Most Read Top Rated Latest

The aggregate functions return a SUM() Function in Oracle SQL Plus single value based on What is Aggregate Function ? groups of rows, rather than single How to use Maximun Aggregate operator in LINQ Partvalue for each row. 1 The aggregate Aggregate Functions in SQL : Part 2 functions ignore null Aggregating Data using Group Functions in Oracle values. We can use Aggregate functions More... in select lists and in ORDER BY and Sponsored by HAVING clauses. Become a Sponsor They are commonly used with the GROUP BY clause in a SELECT statement, where Oracle divides the rows of a queried table or view into groups. All Aggregate Function: 1. 2. 3. 4. 5. 6. 7. 8. AVG Function CORR Function COUNT Function COVAR_POP Function COVER_SAMP Function GROUPING Function MAX Function MIN Function

9. 10. 11. 12. 13. 14. 15. 16.

REGR_(LINEAR REGRESSION) Functions STDDEV Function STDDEV_POP Function STDDEV_SAMP Function SUM Function VAR_POP Function VAR_SAMP Function VARIANCE Function

1. AVG Function: The AVG() function returns average value of expression. We can use it as an aggregate or analytic function. Syntax: SELECT AVG ("Expression") FROM "Table _Name"; Example: The following example calculates the average salary of all employees in the EMP5 table. SQL> SELECT AVG ("SALARY") 2 FROM EMP5; Output:

2. CORR Function: The CORR() function returns the coefficient of correlation of a set of number pairs. We can use it as an aggregate or analytic function. Both expression1 and expression2 are number expressions. Oracle applies the function to the set of (expression1 , expression2) after eliminating the pairs for which either expression1 or expression2 is null. Syntax: CORR( expression1 ,expression2) [ over (analytic_clause) ] Example: SQL> SELECT CORR(SALARY,EMP_ID) 2 FROM EMP5; Output:

3. COUNT Function: The COUNT() function returns the number of rows in the query. We can use it as an aggregate or analytic function. If we specify expression, COUNT returns the number of rows where expression is not null. We can count either all rows, or only distinct values of expression. If we specify the asterisk (*), this function returns all rows, including duplicates and nulls. COUNT never returns null. Syntax: SELECT COUNT(*) FROM Table_Name; Example: SQL> SELECT COUNT(*) "Total Row" 2 FROM EMP5; Output:

4. COVAR_POP() fUNCTION: The COVAR_POP() function returns the population covariance of a set of number pairs. We can use it as an aggregate or analytic function. The cover_pop function as a corr function. Syntax: COVAR_POP(expression1, expression2 ) Both expression1 and expression2 are number expressions. Oracle applies the function to the set of (expression1 , expression2) after eliminating the pairs for which either expression1 or expression2 is null.

Example: SQL> SELECT COVAR_POP(SALARY,EMP_ID) 2 FROM EMP5; Output:

5. COVER_SAMP Function: The COVER_SAMP() function returns the sample covariance of a set of number pairs. We can use it as an aggregate or analytic function. Syntax: COVAR_SAMP(expression1, expression2 ) Both expression1 and expression2 are number expressions. Oracle applies the function to the set of (expression1 , expression2) after eliminating the pairs for which either expression1 or expression2 is null. Example: SQL> SELECT COVAR_SAMP(SALARY,EMP_ID) 2 FROM EMP5; Output:

6. GROUPING Function: The GROUPING() function is applicable only in a select statement that contains a GROUP BY extension, such as ROLLUP or CUBE. These operations produce superaggregate rows that contain nulls representing the set of all values. You can use the GROUPING function to distinguish a null that represents the set of all values in a superaggregate

row from an actual null. Example: SQL> SELECT GROUPING(EMP_NAME) 2 FROM EMP5 3 GROUP BY "EMP_NAME"; Output:

7. MAX Function: The MAX()function returns maximum value of expression. We can use it as an aggregate or analytic function. Example: This example returns the highest salary. SQL> SELECT MAX(SALARY) 2 FROM EMP5; Output:

8. MIN Function: The MIN() function returns minimum value of expression. We can use it as an aggregate or analytic function. Example: This example returns the lowest salary. SQL> SELECT MIN(SALARY) 2 FROM EMP5; Output:

9. REGR_(LINEAR REGRESSION) Functions: The linear regression functions fit an ordinary-least-squares regression line to a set of number pairs. We can use them as both aggregate and analytic functions. The linear regression functions are: REGR_SLOPE REGR_INTERCEPT REGR_COUNT REGR_R2 REGR_AVGX REGR_AVGY REGR_SXX REGR_SYY REGR_SXY Oracle applies the function to the set of (expression1, expression2) pairs after eliminating all pairs for which either expression1 or expression2 is null. Oracle computes all the regression functions simultaneously during a single pass through the data. expression1 is interpreted as a value of the dependent variable (a "y value"), and expression2 is interpreted as a value of the independent variable (an "x value"). REGR_SLOPE() Function: The REGR_SLOPE()function returns the slope of the line. Example: SQL> SELECT REGR_SLOPE(SALARY,EMP_ID) 2 FROM EMP5; Output:

REGR_INTERCEPT Function: The REGR_INTERCEPT()function returns the y-intercept of the regression line. SQL> SELECT REGR_INTERCEPT(SALARY,EMP_ID) 2 FROM EMP5; REGR_COUNT Function: The REGR_COUNT()function returns an integer that is the number of non-null number pairs used to fit the regression line. SQL> SELECT REGR_COUNT(SALARY,EMP_ID) 2 FROM EMP5; REGR_R2 Function: The REGR_R2()function returns the coefficient of determination (also called "R-squared" or "goodness of fit") for the regression. SQL> SELECT REGR_R2(SALARY,EMP_ID) 2 FROM EMP5; REGR_AVGX Function: The REGR_AVGX()function evaluates the average of the independent variable (express12) of the regression line. SQL> SELECT REGR_AVGX(SALARY,EMP_ID) 2 FROM EMP5; REGR_AVGY Function: The REGR_AVGY()function evaluates the average of the dependent variable (express1) of the regression line. SQL> SELECT REGR_AVGY(SALARY,EMP_ID) 2 FROM EMP5; The REGR_SXY(), REGR_SXX(), REGR_SYY() function are auxiliary functions that are used to compute various diagnostic statistics. Example: REGR_SXX Function: SQL> SELECT REGR_SXX(SALARY,EMP_ID) 2 FROM EMP5; REGR_SYY Function: SQL> SELECT REGR_SYY(SALARY,EMP_ID) 2 FROM EMP5; REGR_SXY Function: SQL> SELECT REGR_SXY(SALARY,EMP_ID) 2 FROM EMP5; 10. STDDEV Function: The STDDEV() function returns sample standard deviation of expression, a set of numbers. We can use it as both an aggregate and analytic function. Oracle calculates the standard deviation as the square root of the variance defined for the VARIANCE aggregate function. Example:

SQL> SELECT STDDEV("SALARY") 2 FROM EMP5; Output:

11. STDDEV_POP Function: The STDDEV_POP() function computes the population standard deviation and returns the square root of the population variance Example: The following example returns the population and sample standard deviations of profit from salary in the EMP5 table. SQL> SELECT STDDEV_POP(SALARY) 2 FROM EMP5; Output:

12. STDDEV_SAMP Function: The STDDEV_SAMP() function computes the cumulative sample standard deviation and returns the square root of the sample variance. Example: SQL> SELECT STDDEV_SAMP(SALARY) 2 FROM EMP5; Output:

13. SUM Function: The SUM function returns the summed value of an expression. The expression can be a numeric field and formula. Syntax: SELECT SUM ("Expression") FROM "Table _Name"; Example: SQL> SELECT SUM ("SALARY") 2 FROM EMP5; Output:

14. VAR_POP Function: The VAR_POP() function returns the population variance of a set of numbers after discarding the nulls in this set. Example: SQL> SELECT VAR_POP(SALARY) 2 FROM EMP5; Output:

15. VAR_SAMP Function: The VAR_SAMP() function returns the sample variance of a set of numbers after discarding the nulls in this set Example: SQL> SELECT VAR_SAMP(SALARY) 2 FROM EMP5; Output:

16. VARIANCE Function: The VARIANCE() function returns variance of expression. We can use it as an aggregate or analytic function. Oracle calculates the variance of expression as follows: 0 if the number of rows in expression = 1 VAR_SAMP if the number of rows in expression > 1 Example: The following example calculates the variance of all salaries in the EMP5 table. SQL> SELECT VARIANCE(SALARY) 2 FROM EMP5; Output:

I hope this article help you.

Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article About the author Looking for C # C onsulting? C # C onsulting is founded in 2002 by the founders of C # C orner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC , and Waterfall models. C lick here to learn more about C # C onsulting.

Comment Request!

Sapna Malik

Post a Feedback, Comment, or Question about this article


Subject: Comment:

Sponsored by Become a Sponsor

Submit

Comments

Hosted by | Found a | C ontact| Terms & | Privacy | Site MaximumASP broken link? Us conditions Policy Map 2011 contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.

| Suggest an| Advertise Idea With Us

C urrent Version: 5.2011.3.12

Channels: Jobs | Interviews | C onsulting | Photos | Authors | Tips | Forums | E-Books | Training | Blogs Programming: C # | Visual Basic Sponsors: | ASP.NET & Web Development | C ++ | Other .NET Languages | Windows Vista | XAML | Tutorials