Edit D:\app\Administrator\product\11.2.0\dbhome_1\oc4j\j2ee\oc4j_applications\applications\em\em\online_help\tdddg\tdddg_selecting017.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <script src="./callback.js" type="text/javascript"></script> <noscript>Your browser does not support JavaScript. This help page requires JavaScript to render correctly.</noscript> </head> <meta http-equiv="Content-Type" content="text/html; charset=us-ascii" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <meta name="generator" content="Oracle DARB XHTML Converter (Mode = ohj/ohw) - Version 5.1.1 Build 005" /> <meta name="date" content="2009-04-21T9:46:23Z" /> <meta name="robots" content="noarchive" /> <meta name="doctitle" content="Using Aggregate Functions in Queries" /> <meta name="relnum" content="11g Release 2 (11.2)" /> <meta name="partnum" content="E10766-01" /> <link rel="copyright" href="./dcommon/html/cpyr.htm" title="Copyright" type="text/html" /> <link rel="stylesheet" href="./dcommon/css/blafdoc.css" title="Oracle BLAFDoc" type="text/css" /> <link rel="contents" href="toc.htm" title="Contents" type="text/html" /> <link rel="prev" href="tdddg_selecting016.htm" title="Previous" type="text/html" /> <link rel="next" href="tdddg_selecting018.htm" title="Next" type="text/html" /> <title>Using Aggregate Functions in Queries</title> </head> <body> <div class="zz-skip-header"><a href="#BEGIN">Skip Headers</a></div> <table class="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100%"> <col width="86%" /> <col width="*" /> <tr valign="bottom"> <td align="left"></td> <td align="center"><a href="tdddg_selecting016.htm"><img width="24" height="24" src="./dcommon/gifs/leftnav.gif" alt="Previous" /><br /> <span class="icon">Previous</span></a> </td> <td align="center"><a href="tdddg_selecting018.htm"><img width="24" height="24" src="./dcommon/gifs/rightnav.gif" alt="Next" /><br /> <span class="icon">Next</span></a></td> </tr> </table> <p><a id="BCGJDGBA" name="BCGJDGBA"></a><a id="TDDDG22560" name="TDDDG22560"></a></p> <div class="sect2"><!-- infolevel="all" infotype="General" --> <h1>Using Aggregate Functions in Queries</h1> <a name="BEGIN" id="BEGIN"></a> <p><a id="sthref137" name="sthref137"></a>An aggregate function returns a single result row, based on a group of rows. The group of rows can be an entire table or view. The aggregate functions that SQL supports are listed and described in <a href="javascript:open('http://www.oracle.com/pls/db112/lookup?id=SQLRF20035','newWindow').focus()"><span class="italic">Oracle Database SQL Language Reference</span></a>.</p> <p>Aggregate functions are especially powerful when used with the <a id="sthref138" name="sthref138"></a><a id="sthref139" name="sthref139"></a><code>GROUP</code> <code>BY</code> clause, which groups query results by one or more columns, with a result for each group.</p> <p>The query in the following example uses the <code>COUNT</code> function and the <code>GROUP</code> <code>BY</code> clause to determine how many people report to each manager. The wildcard character, <code>*</code>, represents an entire record.</p> <div class="example"><a id="CHDDGFBF" name="CHDDGFBF"></a><a id="TDDDG166" name="TDDDG166"></a> <p class="titleinexample">Counting the Number of Rows in Each Group</p> <pre xml:space="preserve" class="oac_no_warn"> SELECT MANAGER_ID "Manager", <span class="bold">COUNT(*)</span> "Number of Reports" FROM EMPLOYEES <span class="bold">GROUP BY MANAGER_ID</span>; </pre> <p>Result:</p> <pre xml:space="preserve" class="oac_no_warn"> Manager Number of Reports ---------- ----------------- 100 14 1 123 8 120 8 121 8 147 6 205 1 108 5 148 6 149 6 201 1 Manager Number of Reports ---------- ----------------- 102 1 101 5 114 5 124 8 145 6 146 6 103 4 122 8 19 rows selected. </pre></div> <!-- class="example" --> <p>The preceding example shows that one employee does not report to a manager. The following query selects the first name, last name, and job title of that employee:</p> <pre xml:space="preserve" class="oac_no_warn"> COLUMN FIRST_NAME FORMAT A10; COLUMN LAST_NAME FORMAT A10; COLUMN JOB_TITLE FORMAT A10; SELECT e.FIRST_NAME, e.LAST_NAME, j.JOB_TITLE FROM EMPLOYEES e, JOBS j WHERE e.JOB_ID = j.JOB_ID AND <span class="bold">MANAGER_ID IS NULL</span>; </pre> <p>Result:</p> <pre xml:space="preserve" class="oac_no_warn"> FIRST_NAME LAST_NAME JOB_TITLE ---------- ---------- ---------- Steven King President </pre> <p>When used with the <a id="sthref140" name="sthref140"></a><code>DISTINCT</code> option, the <code>COUNT</code> function shows how many distinct values are in a data set.</p> <p>The two queries in the following example show the total number of departments and the number of departments that have employees.</p> <div class="example"><a id="CHDEJGAF" name="CHDEJGAF"></a><a id="TDDDG167" name="TDDDG167"></a> <p class="titleinexample">Counting the Number of Distinct Values in a Set</p> <pre xml:space="preserve" class="oac_no_warn"> SELECT COUNT(*) FROM DEPARTMENTS; </pre> <p>Result:</p> <pre xml:space="preserve" class="oac_no_warn"> COUNT(*) ---------- 27 SELECT <span class="bold">COUNT(DISTINCT DEPARTMENT_ID)</span> "Number of Departments" FROM EMPLOYEES; </pre> <p>Result:</p> <pre xml:space="preserve" class="oac_no_warn"> Number of Departments --------------------- 11 </pre></div> <!-- class="example" --> <p>The query in the following example uses several aggregate functions to show statistics for the salaries of each <code>JOB_ID</code>.</p> <div class="example"><a id="CHDCEHHE" name="CHDCEHHE"></a><a id="TDDDG168" name="TDDDG168"></a> <p class="titleinexample">Using Aggregate Functions for Statistical Information</p> <pre xml:space="preserve" class="oac_no_warn"> SELECT JOB_ID, <span class="bold">COUNT(*)</span> "#", <span class="bold">MIN(SALARY)</span> "Minimum", <span class="bold">ROUND(AVG(SALARY), 0)</span> "Average", <span class="bold">MEDIAN(SALARY)</span> "Median", <span class="bold">MAX(SALARY)</span> "Maximum", <span class="bold">ROUND(STDDEV(SALARY))</span> "Std Dev" FROM EMPLOYEES <span class="bold">GROUP BY JOB_ID</span> <span class="bold">ORDER BY JOB_ID</span>; </pre> <p>Result:</p> <pre xml:space="preserve" class="oac_no_warn"> JOB_ID # Minimum Average Median Maximum Std Dev ---------- ---------- ---------- ---------- ---------- ---------- ---------- AC_ACCOUNT 1 8300 8300 8300 8300 0 AC_MGR 1 12000 12000 12000 12000 0 AD_ASST 1 4400 4400 4400 4400 0 AD_PRES 1 24000 24000 24000 24000 0 AD_VP 2 17000 17000 17000 17000 0 FI_ACCOUNT 5 6900 7920 7800 9000 766 FI_MGR 1 12000 12000 12000 12000 0 HR_REP 1 6500 6500 6500 6500 0 IT_PROG 5 4200 5760 4800 9000 1926 MK_MAN 1 13000 13000 13000 13000 0 MK_REP 1 6000 6000 6000 6000 0 JOB_ID # Minimum Average Median Maximum Std Dev ---------- ---------- ---------- ---------- ---------- ---------- ---------- PR_REP 1 10000 10000 10000 10000 0 PU_CLERK 5 2500 2780 2800 3100 239 PU_MAN 1 11000 11000 11000 11000 0 SA_MAN 5 10500 12200 12000 14000 1525 SA_REP 30 6100 8350 8200 11500 1524 SH_CLERK 20 2500 3215 3100 4200 548 ST_CLERK 20 2100 2785 2700 3600 453 ST_MAN 5 5800 7280 7900 8200 1066 19 rows selected. </pre></div> <!-- class="example" --> <p>To have the query return only rows where aggregate values meet specified conditions, use the <code>HAVING</code> clause.</p> <p>The query in the following example shows how much each department spends annually on salaries, but only for departments for which that amount exceeds $1,000,000.</p> <div class="example"><a id="CHDIDDDC" name="CHDIDDDC"></a><a id="TDDDG169" name="TDDDG169"></a> <p class="titleinexample">Limiting Aggregate Functions to Rows that Satisfy a Condition</p> <pre xml:space="preserve" class="oac_no_warn"> SELECT DEPARTMENT_ID "Department", SUM(SALARY*12) "All Salaries" FROM EMPLOYEES <span class="bold">HAVING SUM(SALARY * 12) >= 1000000</span> GROUP BY DEPARTMENT_ID; </pre> <p>Result:</p> <pre xml:space="preserve" class="oac_no_warn"> Department All Salaries ---------- ------------ 50 1876800 80 3654000 </pre></div> <!-- class="example" --> <p>The <a id="sthref141" name="sthref141"></a><code>RANK</code> function returns the relative ordered rank of a number, and the <a id="sthref142" name="sthref142"></a><code>PERCENT_RANK</code> function returns the percentile position of a number.</p> <p>The query in the following example shows that a salary of $3,000 is the 20th highest, and is in the 42nd percentile, among all clerks.</p> <div class="example"><a id="CHDCFCCE" name="CHDCFCCE"></a><a id="TDDDG170" name="TDDDG170"></a> <p class="titleinexample">Showing the Rank and Percentile of a Number Within a Group</p> <pre xml:space="preserve" class="oac_no_warn"> SELECT <span class="bold">RANK(3000) WITHIN GROUP</span> <span class="bold">(ORDER BY SALARY DESC)</span> "Rank", ROUND(100 * <span class="bold">(PERCENT_RANK(3000) WITHIN GROUP</span> <span class="bold">(ORDER BY SALARY DESC)</span>), 0) "Percentile" FROM EMPLOYEES WHERE JOB_ID LIKE '%CLERK'; </pre> <p>Result:</p> <pre xml:space="preserve" class="oac_no_warn"> Rank Percentile ---------- ---------- 20 42 </pre></div> <!-- class="example" --> <p>The <code>DENSE_RANK</code> function is like the <code>RANK</code> function, except that the identical values have the same rank, and there are no gaps in the ranking. Using the <code>DENSE_RANK</code> function, $3,000 is the 12<sup>th</sup> highest salary for clerks, as the following example shows.</p> <div class="example"><a id="CHDGIEAD" name="CHDGIEAD"></a><a id="TDDDG171" name="TDDDG171"></a> <p class="titleinexample">Showing the Dense Rank of a Number Within a Group</p> <pre xml:space="preserve" class="oac_no_warn"> SELECT <span class="bold">DENSE_RANK(3000) WITHIN GROUP (ORDER BY salary DESC)</span> "Rank" FROM EMPLOYEES WHERE JOB_ID LIKE '%CLERK'; </pre> <p>Result:</p> <pre xml:space="preserve" class="oac_no_warn"> Rank ---------- 12 </pre></div> <!-- class="example" --> <div class="helpinfonotealso"> <h2>Related Topics</h2> <p><a href="javascript:open('http://www.oracle.com/pls/db112/lookup?id=SQLRF20035','newWindow').focus()"><span class="italic">Oracle Database SQL Language Reference</span></a></p> <p><a href="tdddg_selecting010.htm#BCGCCGJF">Using Operators and Functions in Queries</a></p> </div> </div> <!-- class="sect2" --> <!-- Start Footer --> <div class="footer"> <table class="simple oac_no_warn" summary="" cellspacing="0" cellpadding="0" width="100%"> <col width="86%" /> <col width="*" /> <tr> <td align="left"><span class="copyrightlogo">Copyright © 1996, 2009, Oracle and/or its affiliates. All rights reserved.</span><br /> <a href="./dcommon/html/cpyr.htm"><span class="copyrightlogo">Legal Notices</span></a></td> <td align="center"><a href="tdddg_selecting016.htm"><img width="24" height="24" src="./dcommon/gifs/leftnav.gif" alt="Previous" /><br /> <span class="icon">Previous</span></a> </td> <td align="center"><a href="tdddg_selecting018.htm"><img width="24" height="24" src="./dcommon/gifs/rightnav.gif" alt="Next" /><br /> <span class="icon">Next</span></a></td> </tr> </table> </div> <!-- class="footer" --> </body> </html>
Ms-Dos/Windows
Unix
Write backup
jsp File Browser version 1.2 by
www.vonloesch.de