<html>
<head>
<link rel="stylesheet" href="josh.css">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<DIV id="DESCRIPTION">
  <table width="100%" cellspacing="0" cellpadding="0" border="0">
    <tr>
      <td valign="top" class="name">Aggregate Functions</td>
      <td valign="top" nowrap class="compatibility"> </td>
    </tr>
    <tr>
      <td colspan="2" class="divider"><img src="dwres:18084" width="100%" height="1"></td>
    </tr>
    <tr>
      <td valign="top" colspan="2" class="description"> <p>Aggregate functions
          return a single value based upon a set of other values. If used among
          many other expressions in the item list of a <span class="emphasis">SELECT</span> statement,
          the <span class="emphasis">SELECT</span> must have a <span class="emphasis">GROUP
          BY</span> clause. No <span class="emphasis">GROUP BY</span> clause
          is required if the aggregate function is the only value retrieved by
          the <span class="emphasis">SELECT</span> statement. The supported aggregate
          functions and their syntax are listed in Table 4.1 .</p></td>
    </tr>
    <tr>
      <td valign="top" colspan="2" class="CLEARSEPARATION">&nbsp;</td>
    </tr>
    <tr>
      <td valign="top" colspan="2" class="description"><span class="title">SQL99
          Aggregate Functions</span> </td>
    </tr>
    <tr>
      <td valign="top" colspan="2" class="CLEARSEPARATION">&nbsp;</td>
    </tr>
    <tr>
      <td> <table border="1" >
          <tr>
            <th>Function</th>
            <th>Usage</th>
          </tr>
          <tr>
            <td> AVG(expression) </td>
            <td> Computes the average value of a column by the expression </td>
          </tr>
          <tr>
            <td> COUNT(expression) </td>
            <td> Counts the rows defined by the expression </td>
          </tr>
          <tr>
            <td> COUNT(*) </td>
            <td> Counts all rows in the specified table or view </td>
          </tr>
          <tr>
            <td> MIN(expression) </td>
            <td> Finds the minimum value in a column by the expression </td>
          </tr>
          <tr>
            <td> MAX(expression) </td>
            <td> Finds the maximum value in a column by the expression </td>
          </tr>
          <tr>
            <td> SUM(expression) </td>
            <td> Computes the sum of column values by the expression </td>
          </tr>
        </table></td>
    </tr>
    <tr>
      <td valign="top" colspan="2" class="CLEARSEPARATION">&nbsp;</td>
    </tr>
    <tr>
      <td> <p>Technically speaking, ANY, EVERY, and SOME are considered aggregate
          functions. However, they have been discussed as range search criteria
          since they are most often used that way. Refer to the SELECT . . .
          WHERE topic in the previous chapter for more information on these functions.</p>
        <p>The number of values processed by an aggregate varies depending on
          the number of rows queried from the table. This behavior makes aggregate
          functions different from scalar functions, which require a fixed number
          and fixed type of parameters.</p>
        <p>The general syntax of an aggregate function is:</p>
        <span class="programlisting">aggregate_function_name ( [ALL | DISTINCT]
        expression )</span>
        <p>The aggregate function name may be AVG, COUNT, MAX, MIN, or SUM. The<command> </command>ALL<command> </command>clause,
          which is the default behavior and does not actually need to be specified,
          evaluates all rows when aggregating the value of the function. The
          DISTINCT clause uses only distinct values when evaluating the function.</p></td>
    </tr>
  </table>
</DIV>
<DIV id="AVG and SUM">
  <table border="0" cellspacing="0" cellpadding="0" width="100%">
    <tr>
      <td valign="top" class="name">AVG and SUM</td>
      <td valign="top" nowrap class="compatibility"> </td>
    </tr>
    <tr>
      <td colspan="2" class="divider"><img src="dwres:18084" width="100%" height="1"></td>
    </tr>
    <tr>
      <td valign="top" colspan="2" class="description"> <p>The <indexterm id="IXT-4-154141"><primary>AVG
              function</primary></indexterm> computes the average of values in
              a column or an expression. <indexterm id="IXT-4-154143"><primary>SUM
              function</primary></indexterm> computes the sum. Both functions
              work with numeric values and ignore NULL values. They also can
              be used to compute the average or sum of all values of a column
              or expression.</p>
        <p>AVG and SUM are supported by Microsoft SQL Server, MySQL, Oracle,
          and PostgreSQL.</p></td>
    </tr>
    <tr>
      <td valign="top" colspan="2" class="CLEARSEPARATION">&nbsp;</td>
    </tr>
    <tr>
      <td valign="top" colspan="2" class="example"><span class="title">Example</span> </td>
    </tr>
    <tr>
      <td> <p>The following query computes average year-to-date sales for each
          type of book:</p>
        <span class="programlisting">
        <pre>SELECT   type, AVG( ytd_sales ) AS "average_ytd_sales"
FROM     titles
GROUP BY type;</pre>
        </span>
        <p>This query returns the sum of year-to-date sales for each type of
          book:</p>
        <span class="programlisting">
        <pre>SELECT   type, SUM( ytd_sales )
FROM     titles
GROUP BY type;</pre>
        </span> </td>
    </tr>
  </table>
</DIV>
<DIV id="COUNT">
  <table border="0" cellspacing="0" cellpadding="0" width="100%">
    <tr>
      <td valign="top" class="name">COUNT</td>
      <td valign="top" nowrap class="compatibility"> </td>
    </tr>
    <tr>
      <td colspan="2" class="divider"><img src="dwres:18084" width="100%" height="1"></td>
    </tr>
    <tr>
      <td valign="top" colspan="2" class="description"> <p>The COUNT function
          has three variations. COUNT(*) counts all the rows in the target table
          whether they include nulls or not. COUNT(expression) computes the number
          of rows with non-NULL values in a specific column or expression. COUNT(DISTINCT
          expression) computes the number of distinct non-NULL values in a column
          or expression.</p></td>
    </tr>
    <tr>
      <td valign="top" colspan="2" class="CLEARSEPARATION">&nbsp;</td>
    </tr>
    <tr>
      <td valign="top" colspan="2" class="example"><span class="title">Examples</span> </td>
    </tr>
    <tr>
      <td> <p>This query counts all rows in a table:</p>
        <span class="programlisting">
        <pre>SELECT COUNT(*) FROM publishers;</pre>
        </span>
        <p>The following query finds the number of different countries where
          publishers are located:</p>
        <span class="programlisting">
        <pre>SELECT COUNT(DISTINCT country) "Count of Countries"
FROM   publishers</pre>
        </span> </td>
    </tr>
  </table>
</DIV>
<DIV id="MIN and MAX">
  <table border="0" cellspacing="0" cellpadding="0" width="100%">
    <tr>
      <td valign="top" class="name">MIN and MAX</td>
      <td valign="top" nowrap class="compatibility"> </td>
    </tr>
    <tr>
      <td colspan="2" class="divider"><img src="dwres:18084" width="100%" height="1"></td>
    </tr>
    <tr>
      <td valign="top" colspan="2" class="description"> <p>MIN(expression) and
          MAX(expression) find the minimum and maximum value (string, datetime,
          or numeric) in a set of rows. DISTINCT or ALL may be used with these
          functions, but they do not affect the result.</p>
        <p>MIN and MAX are supported by Microsoft SQL Server, MySQL, Oracle,
          and PostgreSQL.</p>
        <p>MySQL also supports the functions LEAST( ) and GREATEST( ), providing
          the same capabilities.</p></td>
    </tr>
    <tr>
      <td valign="top" colspan="2" class="CLEARSEPARATION">&nbsp;</td>
    </tr>
    <tr>
      <td valign="top" colspan="2" class="example"><span class="title">Examples</span> </td>
    </tr>
    <tr>
      <td> <p>The following query finds the best and worst sales for any title
          on record:</p>
        <span class="programlisting">
        <pre>SELECT  'MIN' = MIN(ytd_sales), 'MAX' = MAX(ytd_sales)
FROM    titles;</pre>
        </span>
        <p>Aggregate functions are used often in the <indexterm id="IXT-4-154155"><primary>HAVING
              clause</primary></indexterm> of queries with GROUP BY. The following
              query selects all categories (types) of books that have an average
              price for all books in the category higher than $15.00:</p>
        <span class="programlisting">
        <pre>SELECT  type 'Category', AVG( price ) 'Average Price'
FROM    titles
GROUP BY type
HAVING AVG(price) &gt; 15</pre>
        </span> </td>
    </tr>
  </table>
</DIV>
</body>
</html>
