584 lines
35 KiB
HTML
584 lines
35 KiB
HTML
|
<?xml version="1.0" encoding="utf-8"?>
|
||
|
<!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" lang="en-US" xml:lang="en-us">
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||
|
<meta name="dc.language" scheme="rfc1766" content="en-us" />
|
||
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
||
|
<!-- US Government Users Restricted Rights -->
|
||
|
<!-- Use, duplication or disclosure restricted by -->
|
||
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
||
|
<meta name="dc.date" scheme="iso8601" content="2005-09-19" />
|
||
|
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
|
||
|
<meta name="security" content="public" />
|
||
|
<meta name="Robots" content="index,follow"/>
|
||
|
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
|
||
|
<meta name="keywords" content="aggregate function, aggregate, function,
|
||
|
column function, column, AVG function, keyword, ALL clause, AVG, DISTINCT,
|
||
|
COUNT function, * (asterisk), COUNT, in COUNT function, asterisk (*),
|
||
|
COUNT_BIG function, COUNT_BIG, in COUNT_BIG function, MAX function, MAX,
|
||
|
MIN function, MIN, STDDEV_POP function, STDDEV_POP, STDDEV function, STDDEV,
|
||
|
STDDEV_SAMP function, STDDEV_SAMP, SUM function, SUM, VAR function,
|
||
|
VARIANCE function, VAR_POP function, VAR_POP, VARIANCE, VAR,
|
||
|
VARIANCE_SAMP function, VAR_SAMP function, VAR_SAMP, VARIANCE_SAMP" />
|
||
|
<title>Aggregate functions</title>
|
||
|
<link rel="stylesheet" type="text/css" href="ibmidwb.css" />
|
||
|
<link rel="stylesheet" type="text/css" href="ic.css" />
|
||
|
</head>
|
||
|
<body>
|
||
|
<a id="Top_Of_Page" name="Top_Of_Page"></a><!-- Java sync-link -->
|
||
|
<script language = "Javascript" src = "../rzahg/synch.js" type="text/javascript"></script>
|
||
|
|
||
|
|
||
|
<a name="colfunc"></a>
|
||
|
<h2 id="colfunc"><a href="rbafzmst02.htm#ToC_316">Aggregate functions</a></h2><a id="idx786" name="idx786"></a><a id="idx787" name="idx787"></a><a id="idx788" name="idx788"></a><a id="idx789" name="idx789"></a>
|
||
|
<p>The following information applies to all aggregate functions other than
|
||
|
COUNT(*) and COUNT_BIG(*).</p>
|
||
|
<ul>
|
||
|
<li>The argument of an aggregate function is a set of values derived
|
||
|
from an expression. The expression may include columns but cannot include
|
||
|
another aggregate function. The scope of the set is a group or an intermediate
|
||
|
result table as explained in Chapter 4, "Queries".</li>
|
||
|
<li>If a GROUP BY clause is specified in a query and the intermediate
|
||
|
result of the FROM, WHERE, GROUP BY, and HAVING clauses is the empty set,
|
||
|
then the aggregate functions are not applied, the result of the query is the
|
||
|
empty set.</li>
|
||
|
<li>If a GROUP BY clause is not specified in a query and the intermediate
|
||
|
result of the FROM, WHERE, and HAVING clauses is the empty set, then the aggregate
|
||
|
functions are applied to the empty set. For example, the result of the following
|
||
|
SELECT statement is applied to the empty set because department D01 has no
|
||
|
employees:
|
||
|
<pre class="xmp"> <span class="bold">SELECT COUNT</span>(<span class="bold">DISTINCT</span> JOB)
|
||
|
<span class="bold">FROM</span> EMPLOYEE
|
||
|
<span class="bold">WHERE</span> WORKDEPT = 'D01'</pre></li>
|
||
|
<li>The keyword DISTINCT is not considered an argument of the function,
|
||
|
but rather a specification of an operation that is performed before the function
|
||
|
is applied. If DISTINCT is specified, redundant duplicate values are eliminated.
|
||
|
If ALL is implicitly or explicitly specified, redundant duplicate values are
|
||
|
not eliminated.</li>
|
||
|
<li>An aggregate function can be used in a WHERE clause only if that clause
|
||
|
is part of a subquery of a HAVING clause and the column name specified in
|
||
|
the expression is a correlated reference to a group. If the expression includes
|
||
|
more than one column name, each column name must be a correlated reference
|
||
|
to the same group.</li></ul>
|
||
|
<a name="avg"></a>
|
||
|
<h3 id="avg"><a href="rbafzmst02.htm#ToC_317">AVG</a></h3><a id="idx790" name="idx790"></a><a id="idx791" name="idx791"></a><a id="idx792" name="idx792"></a><a id="idx793" name="idx793"></a>
|
||
|
<a href="rbafzmstcolfunc.htm#avgdesc"><img src="c.gif" alt="Click to skip syntax diagram" /></a>
|
||
|
<a name="wq483"></a>
|
||
|
<div class="fignone" id="wq483">
|
||
|
<pre class="cgraphic"><span><img src="c.gif" alt="Read syntax diagram" longdesc="rbafzmstsyn38.htm"
|
||
|
border="0" /></span><a href="#skipsyn-37"><img src="c.gif" alt="Skip visual syntax diagram"
|
||
|
border="0" /></a> .-ALL------.
|
||
|
>>-AVG--(--+----------+--<span class="italic">numeric-expression</span>--)-----------------><
|
||
|
'-DISTINCT-'
|
||
|
|
||
|
</pre>
|
||
|
<a name="skipsyn-37" id="skipsyn-37"></a></div>
|
||
|
<a name="avgdesc"></a>
|
||
|
<p id="avgdesc">The AVG function returns the average of a set of numbers.</p>
|
||
|
<dl class="parml">
|
||
|
<dt class="bold"><var class="pv">numeric-expression</var></dt>
|
||
|
<dd>The argument values must be any built-in numeric data type and their
|
||
|
sum must be within the range of the data type of the result.
|
||
|
</dd>
|
||
|
</dl>
|
||
|
<p>The data type of the result is the same as the data type of the argument
|
||
|
values, except that: </p>
|
||
|
<ul>
|
||
|
<li>The result is double-precision floating point if the argument values are single-precision
|
||
|
floating point.</li>
|
||
|
<li>The result is large integer if the argument values are small integers.</li>
|
||
|
<li>The result is decimal if the argument values are decimal or nonzero scale
|
||
|
binary with precision <span class="italic">p</span> and scale <span class="italic">s</span>. The precision of the result is <span class="italic">p</span>-<span class="italic">s</span>+ min(<span class="italic">ms</span>, <span class="italic">mp</span>-<span class="italic">p</span>+<span class="italic">s</span>). The scale of the result is min(<span class="italic">ms</span>, <span class="italic">mp</span>-<span class="italic">p</span>+<span class="italic">s</span>).</li></ul><p class="indatacontent">For information on the values of <span class="italic">p</span>, <span class="italic">s</span>, <span class="italic">ms</span>, and <span class="italic">mp</span>, see <a href="rbafzmstch2expr.htm#decarithmetic">Decimal arithmetic in SQL</a>.</p>
|
||
|
<p>The function is applied to the set of values derived from the argument
|
||
|
values by the elimination of null values. If DISTINCT is used, duplicate values
|
||
|
are eliminated.</p>
|
||
|
<p>The result can be null. If set of values is empty, the result is the null
|
||
|
value. Otherwise, the result is the average value of the set.</p>
|
||
|
<p>The order in which the values are aggregated is undefined, but every intermediate
|
||
|
result must be within the range of the result data type.</p>
|
||
|
<p>If the type of the result is integer, the fractional part of the average
|
||
|
is lost.</p>
|
||
|
<a name="wq484"></a>
|
||
|
<h4 id="wq484">Examples</h4>
|
||
|
<ul>
|
||
|
<li>Using the PROJECT table, set the host variable AVERAGE (DECIMAL(5,2))
|
||
|
to the average staffing level (PRSTAFF) of projects in department (DEPTNO) 'D11'.
|
||
|
<pre class="xmp"> <span class="bold">SELECT AVG(</span>PRSTAFF<span class="bold">)
|
||
|
INTO</span> :AVERAGE
|
||
|
<span class="bold">FROM</span> PROJECT
|
||
|
<span class="bold">WHERE</span> DEPTNO = 'D11'</pre> Results
|
||
|
in AVERAGE being set to 4.25 (that is, 17/4).</li>
|
||
|
<li>Using the PROJECT table, set the host variable ANY_CALC to the average
|
||
|
of each unique staffing value (PRSTAFF) of projects in department (DEPTNO)
|
||
|
'D11'.
|
||
|
<pre class="xmp"> <span class="bold">SELECT AVG</span>(<span class="bold">DISTINCT</span> PRSTAFF)
|
||
|
<span class="bold">INTO</span> :ANY_CALC
|
||
|
<span class="bold">FROM</span> PROJECT
|
||
|
<span class="bold">WHERE</span> DEPTNO = 'D11'</pre>Results
|
||
|
in ANY_CALC being set to 4.66 (that is, 14/3).</li></ul>
|
||
|
<a name="count"></a>
|
||
|
<h3 id="count"><a href="rbafzmst02.htm#ToC_319">COUNT</a></h3><a id="idx794" name="idx794"></a><a id="idx795" name="idx795"></a><a id="idx796" name="idx796"></a><a id="idx797" name="idx797"></a><a id="idx798" name="idx798"></a>
|
||
|
<a href="rbafzmstcolfunc.htm#syncount"><img src="c.gif" alt="Click to skip syntax diagram" /></a>
|
||
|
<a name="wq485"></a>
|
||
|
<div class="fignone" id="wq485">
|
||
|
<pre class="cgraphic"><span><img src="c.gif" alt="Read syntax diagram" longdesc="rbafzmstsyn39.htm"
|
||
|
border="0" /></span><a href="#skipsyn-38"><img src="c.gif" alt="Skip visual syntax diagram"
|
||
|
border="0" /></a> .-ALL------.
|
||
|
>>-COUNT--(--+-+----------+--<span class="italic">expression</span>-+--)-------------------><
|
||
|
| '-DISTINCT-' |
|
||
|
'-*------------------------'
|
||
|
|
||
|
</pre>
|
||
|
<a name="skipsyn-38" id="skipsyn-38"></a></div>
|
||
|
<a name="syncount"></a>
|
||
|
<p id="syncount">The COUNT function returns the number of rows or values in
|
||
|
a set of rows or values.</p>
|
||
|
<dl class="parml">
|
||
|
<dt class="bold"><var class="pv">expression</var></dt>
|
||
|
<dd>The argument values can be of any built-in data type other
|
||
|
than a DataLink. If DISTINCT is used, the resulting <var class="pv">expression</var> must
|
||
|
not have a length attribute greater than 2000 for a character column or 1000
|
||
|
for a graphic column, and must not be a LOB.
|
||
|
</dd>
|
||
|
</dl>
|
||
|
<p>The result of the function is a large integer and it must be within the
|
||
|
range of large integers. The result cannot be null. If the table is a distributed
|
||
|
table, then the result is DECIMAL(15,0). For more information about distributed
|
||
|
tables, see the <a href="../dbmult/rzaf3kickoff.htm">DB2® Multisystem</a> book.</p><a id="idx799" name="idx799"></a><a id="idx800" name="idx800"></a>
|
||
|
<p>The argument of COUNT(*) is a set of rows. The result is the number of
|
||
|
rows in the set. A row that includes only null values is included in the count.</p>
|
||
|
<p>The argument of COUNT(<var class="pv">expression</var>) or COUNT(ALL <var class="pv">expression</var>) is a set of values. The function is applied to the set derived from
|
||
|
the argument values by the elimination of null values. The result is the number
|
||
|
of non-null values in the set including duplicates.</p>
|
||
|
<p>The argument of COUNT(DISTINCT <var class="pv">expression</var>) is a set of values.
|
||
|
The function is applied to the set of values derived from the argument values
|
||
|
by the elimination of null values and duplicate values. The result is the
|
||
|
number of values in the set.</p>
|
||
|
<p>If a sort sequence other than *HEX is in effect when the statement that
|
||
|
contains the COUNT(DISTINCT <var class="pv">expression</var>) is executed and the arguments
|
||
|
are SBCS data, mixed data, or Unicode data, then the result is obtained by
|
||
|
comparing weighted values for each value in the set. The weighted values
|
||
|
are based on the sort sequence.</p>
|
||
|
<a name="wq486"></a>
|
||
|
<h4 id="wq486">Examples</h4>
|
||
|
<ul>
|
||
|
<li>Using the EMPLOYEE table, set the host variable FEMALE (INTEGER) to the
|
||
|
number of rows where the value of the SEX column is 'F'.
|
||
|
<pre class="xmp"> <span class="bold">SELECT COUNT(*)</span>
|
||
|
<span class="bold">INTO</span> :FEMALE
|
||
|
<span class="bold">FROM</span> EMPLOYEE
|
||
|
<span class="bold">WHERE</span> SEX = 'F'</pre>Results in FEMALE being set
|
||
|
to 19.</li>
|
||
|
<li>Using the EMPLOYEE table, set the host variable FEMALE_IN_DEPT (INTEGER)
|
||
|
to the number of departments (WORKDEPT) that have at least one female as a
|
||
|
member.
|
||
|
<pre class="xmp"> <span class="bold">SELECT COUNT</span>(<span class="bold">DISTINCT</span> WORKDEPT)
|
||
|
<span class="bold">INTO</span> :FEMALE_IN_DEPT
|
||
|
<span class="bold">FROM</span> EMPLOYEE
|
||
|
<span class="bold">WHERE</span> SEX='F'</pre>Results in
|
||
|
FEMALE_IN_DEPT being set to 6. (There is at least one female in departments
|
||
|
A00, C01, D11, D21, E11, and E21.)</li></ul>
|
||
|
<a name="countbig"></a>
|
||
|
<h3 id="countbig"><a href="rbafzmst02.htm#ToC_321">COUNT_BIG</a></h3><a id="idx801" name="idx801"></a><a id="idx802" name="idx802"></a><a id="idx803" name="idx803"></a><a id="idx804" name="idx804"></a><a id="idx805" name="idx805"></a>
|
||
|
<a href="rbafzmstcolfunc.htm#syncount_big"><img src="c.gif" alt="Click to skip syntax diagram" /></a>
|
||
|
<a name="wq487"></a>
|
||
|
<div class="fignone" id="wq487">
|
||
|
<pre class="cgraphic"><span><img src="c.gif" alt="Read syntax diagram" longdesc="rbafzmstsyn40.htm"
|
||
|
border="0" /></span><a href="#skipsyn-39"><img src="c.gif" alt="Skip visual syntax diagram"
|
||
|
border="0" /></a> .-ALL------.
|
||
|
>>-COUNT_BIG--(--+-+----------+--<span class="italic">expression</span>-+--)---------------><
|
||
|
| '-DISTINCT-' |
|
||
|
'-*------------------------'
|
||
|
|
||
|
</pre>
|
||
|
<a name="skipsyn-39" id="skipsyn-39"></a></div>
|
||
|
<a name="syncount_big"></a>
|
||
|
<p id="syncount_big">The COUNT_BIG function returns the number of rows or
|
||
|
values in a set of rows or values. It is similar to COUNT except that the
|
||
|
result can be greater than the maximum value of integer.</p>
|
||
|
<dl class="parml">
|
||
|
<dt class="bold"><var class="pv">expression</var></dt>
|
||
|
<dd>The argument values can be of any built-in data type other
|
||
|
than a DataLink. If DISTINCT is used, the resulting <var class="pv">expression</var> must
|
||
|
not have a length attribute greater than 2000 for a character column or 1000
|
||
|
for a graphic column, and must not be a LOB.
|
||
|
</dd>
|
||
|
</dl>
|
||
|
<p>The result of the function is a decimal with precision 31 and scale 0.
|
||
|
The result cannot be null.</p><a id="idx806" name="idx806"></a><a id="idx807" name="idx807"></a>
|
||
|
<p>The argument of COUNT_BIG(*) is a set of rows. The result is the number
|
||
|
of rows in the set. A row that includes only null values is included in the
|
||
|
count.</p>
|
||
|
<p>The argument of COUNT_BIG(<var class="pv">expression</var>) is a set of values. The
|
||
|
function is applied to the set derived from the argument values by the elimination
|
||
|
of null values. The result is the number of values in the set.</p>
|
||
|
<p>If a sort sequence other than *HEX is in effect when the statement that
|
||
|
contains the COUNT_BIG(DISTINCT <var class="pv">expression</var>) is executed and the arguments
|
||
|
are SBCS data, mixed data, or Unicode data, then the result is obtained by
|
||
|
comparing weighted values for each value in the set. The weighted values
|
||
|
are based on the sort sequence.</p>
|
||
|
<a name="wq488"></a>
|
||
|
<h4 id="wq488">Examples</h4>
|
||
|
<ul>
|
||
|
<li>Refer to COUNT examples and substitute COUNT_BIG for occurrences of COUNT.
|
||
|
The results are the same except for the data type of the result.</li>
|
||
|
<li>To count on a specific column, a sourced function must specify the type
|
||
|
of the column. In this example, the CREATE FUNCTION statement creates a sourced
|
||
|
function that takes any column defined as CHAR, uses COUNT_BIG to perform
|
||
|
the counting, and returns the result as a double precision floating-point
|
||
|
number. The query shown counts the number of unique departments in the sample
|
||
|
employee table.
|
||
|
<pre class="xmp"> <span class="bold">CREATE FUNCTION</span> RICK.COUNT(CHAR()) <span class="bold">RETURNS DOUBLE</span>
|
||
|
<span class="bold">SOURCE</span> QSYS2.COUNT_BIG(CHAR());
|
||
|
|
||
|
<span class="bold">SET CURRENT PATH</span> RICK, <span class="bold">SYSTEM PATH</span>
|
||
|
|
||
|
<span class="bold">SELECT COUNT(DISTINCT</span> WORKDEPT <span class="bold">FROM</span> EMPLOYEE;</pre>The empty parenthesis in the parameter list for the
|
||
|
new function (RICK.COUNT) means that the input parameter for the new function
|
||
|
is the same type as the input parameter for the function named in the SOURCE
|
||
|
clause. The empty parenthesis in the parameter list in the SOURCE clause (COUNT_BIG)
|
||
|
means that the length attribute of the CHAR parameter of the COUNT_BIG function
|
||
|
is ignored when DB2 locates the COUNT_BIG function.</li></ul>
|
||
|
<a name="colmax"></a>
|
||
|
<h3 id="colmax"><a href="rbafzmst02.htm#ToC_323">MAX</a></h3><a id="idx808" name="idx808"></a><a id="idx809" name="idx809"></a><a id="idx810" name="idx810"></a><a id="idx811" name="idx811"></a>
|
||
|
<a href="rbafzmstcolfunc.htm#synmax"><img src="c.gif" alt="Click to skip syntax diagram" /></a>
|
||
|
<a name="wq489"></a>
|
||
|
<div class="fignone" id="wq489">
|
||
|
<pre class="cgraphic"><span><img src="c.gif" alt="Read syntax diagram" longdesc="rbafzmstsyn41.htm"
|
||
|
border="0" /></span><a href="#skipsyn-40"><img src="c.gif" alt="Skip visual syntax diagram"
|
||
|
border="0" /></a> .-ALL------.
|
||
|
>>-MAX--(--+----------+--<span class="italic">expression</span>--)-------------------------><
|
||
|
'-DISTINCT-'
|
||
|
|
||
|
</pre>
|
||
|
<a name="skipsyn-40" id="skipsyn-40"></a></div>
|
||
|
<a name="synmax"></a>
|
||
|
<p id="synmax">The MAX aggregate function returns the maximum value
|
||
|
in a set of values in a group.</p>
|
||
|
<dl class="parml">
|
||
|
<dt class="bold"><var class="pv">expression</var></dt>
|
||
|
<dd>The argument values can be any built-in data types except LOB and DataLink
|
||
|
values.
|
||
|
</dd>
|
||
|
</dl>
|
||
|
<p>The data type and length attribute of the result are the same as the data
|
||
|
type and length attribute of the argument values. When the argument is a string,
|
||
|
the result has the same CCSID as the argument.</p>
|
||
|
<p>If a sort sequence other than *HEX is in effect when the statement that
|
||
|
contains the MAX function is executed and the arguments are SBCS data, mixed
|
||
|
data, or Unicode data, then the result is obtained by comparing weighted values
|
||
|
for each value in the set. The weighted values are based on the sort sequence.</p>
|
||
|
<p>The function is applied to the set of values derived from the argument
|
||
|
values by the elimination of null values.</p>
|
||
|
<p> The result can be null. If the function is applied to the empty set, the
|
||
|
result is a null value. Otherwise, the result is the maximum value in the
|
||
|
set.</p>
|
||
|
<p>The specification of DISTINCT has no effect on the result and is not advised.</p>
|
||
|
<a name="wq490"></a>
|
||
|
<h4 id="wq490">Examples</h4>
|
||
|
<ul>
|
||
|
<li>Using the EMPLOYEE table, set the host variable MAX_SALARY (DECIMAL(7,2))
|
||
|
to the maximum monthly salary (SALARY / 12) value.
|
||
|
<pre class="xmp"> <span class="bold">SELECT MAX(</span>SALARY<span class="bold">) /</span>12
|
||
|
<span class="bold">INTO</span> :MAX_SALARY
|
||
|
<span class="bold">FROM</span> EMPLOYEE</pre> Results in
|
||
|
MAX_SALARY being set to 4395.83.</li>
|
||
|
<li>Using the PROJECT table, set the host variable LAST_PROJ (CHAR(24))
|
||
|
to the project name (PROJNAME) that comes last in the sort sequence.
|
||
|
<pre class="xmp"> <span class="bold">SELECT MAX(</span>PROJNAME<span class="bold">)
|
||
|
INTO</span> :LAST_PROJ
|
||
|
<span class="bold">FROM</span> PROJECT</pre>Results in
|
||
|
LAST_PROJ being set to 'WELD LINE PLANNING '.</li></ul>
|
||
|
<a name="colmin"></a>
|
||
|
<h3 id="colmin"><a href="rbafzmst02.htm#ToC_325">MIN</a></h3><a id="idx812" name="idx812"></a><a id="idx813" name="idx813"></a><a id="idx814" name="idx814"></a><a id="idx815" name="idx815"></a>
|
||
|
<a href="rbafzmstcolfunc.htm#synmin"><img src="c.gif" alt="Click to skip syntax diagram" /></a>
|
||
|
<a name="wq491"></a>
|
||
|
<div class="fignone" id="wq491">
|
||
|
<pre class="cgraphic"><span><img src="c.gif" alt="Read syntax diagram" longdesc="rbafzmstsyn42.htm"
|
||
|
border="0" /></span><a href="#skipsyn-41"><img src="c.gif" alt="Skip visual syntax diagram"
|
||
|
border="0" /></a> .-ALL------.
|
||
|
>>-MIN--(--+----------+--<span class="italic">expression</span>--)-------------------------><
|
||
|
'-DISTINCT-'
|
||
|
|
||
|
</pre>
|
||
|
<a name="skipsyn-41" id="skipsyn-41"></a></div>
|
||
|
<a name="synmin"></a>
|
||
|
<p id="synmin">The MIN aggregate function returns the minimum value
|
||
|
in a set of values in a group.</p>
|
||
|
<dl class="parml">
|
||
|
<dt class="bold"><var class="pv">expression</var></dt>
|
||
|
<dd>The argument values can be any built-in data types except LOB and DataLink
|
||
|
values.
|
||
|
</dd>
|
||
|
</dl>
|
||
|
<p>The data type and length attribute of the result are the same as the data
|
||
|
type and length attribute of the argument values. When the argument is a string,
|
||
|
the result has the same CCSID as the argument. The result can be null.</p>
|
||
|
<p>If a sort sequence other than *HEX is in effect when the statement that
|
||
|
contains the MIN function is executed and the arguments are SBCS data, mixed
|
||
|
data, or Unicode data, then the result is obtained by comparing weighted values
|
||
|
for each value in the set.</p>
|
||
|
<p>The function is applied to the set of values derived from the argument
|
||
|
values by the elimination of null values.</p>
|
||
|
<p>If the function is applied to the empty set, the result is a null value.
|
||
|
Otherwise, the result is the minimum value in the set.</p>
|
||
|
<p>The specification of DISTINCT has no effect on the result and is not advised.</p>
|
||
|
<a name="wq492"></a>
|
||
|
<h4 id="wq492">Examples</h4>
|
||
|
<ul>
|
||
|
<li>Using the EMPLOYEE table, set the host variable COMM_SPREAD (DECIMAL(7,2))
|
||
|
to the difference between the maximum and minimum commission (COMM) for the
|
||
|
members of department (WORKDEPT) 'D11'.
|
||
|
<pre class="xmp"> <span class="bold">SELECT MAX(</span>COMM<span class="bold">) - MIN(</span>COMM<span class="bold">)
|
||
|
INTO</span> :COMM_SPREAD
|
||
|
<span class="bold">FROM</span> EMPLOYEE
|
||
|
<span class="bold">WHERE</span> WORKDEPT = 'D11'</pre> Results
|
||
|
in COMM_SPREAD being set to 1118 (that is, 2580 - 1462).</li>
|
||
|
<li>Using the PROJECT table, set the host variable FIRST_FINISHED (CHAR(10))
|
||
|
to the estimated ending date (PRENDATE) of the first project scheduled to
|
||
|
be completed.
|
||
|
<pre class="xmp"> <span class="bold">SELECT MIN(</span>PRENDATE<span class="bold">)
|
||
|
INTO</span> :FIRST_FINISHED
|
||
|
<span class="bold">FROM</span> PROJECT</pre> Results in
|
||
|
FIRST_FINISHED being set to '1982-09-15'.</li></ul>
|
||
|
<a name="colstddev"></a>
|
||
|
<h3 id="colstddev"><a href="rbafzmst02.htm#ToC_327">STDDEV_POP or STDDEV</a></h3><a id="idx816" name="idx816"></a><a id="idx817" name="idx817"></a><a id="idx818" name="idx818"></a><a id="idx819" name="idx819"></a><a id="idx820" name="idx820"></a><a id="idx821" name="idx821"></a><a id="idx822" name="idx822"></a><a id="idx823" name="idx823"></a>
|
||
|
<a href="rbafzmstcolfunc.htm#synstddev"><img src="c.gif" alt="Click to skip syntax diagram" /></a>
|
||
|
<a name="wq493"></a>
|
||
|
<div class="fignone" id="wq493">
|
||
|
<pre class="cgraphic"><span><img src="c.gif" alt="Read syntax diagram" longdesc="rbafzmstsyn43.htm"
|
||
|
border="0" /></span><a href="#skipsyn-42"><img src="c.gif" alt="Skip visual syntax diagram"
|
||
|
border="0" /></a> .-ALL------.
|
||
|
>>-+-STDDEV_POP-+--(--+----------+--<span class="italic">numeric-expression</span>--)------><
|
||
|
'-STDDEV-----' '-DISTINCT-'
|
||
|
|
||
|
</pre>
|
||
|
<a name="skipsyn-42" id="skipsyn-42"></a></div>
|
||
|
<a name="synstddev"></a>
|
||
|
<p id="synstddev">The STDDEV_POP function returns the biased standard deviation
|
||
|
(/n) of a set of numbers. The formula used to calculate the biased standard
|
||
|
deviation is: </p>
|
||
|
<pre class="xmp">STDDEV_POP = SQRT(VAR_POP)</pre><p class="indatacontent"> where SQRT(VAR_POP) is
|
||
|
the square root of the variance.</p>
|
||
|
<dl class="parml">
|
||
|
<dt class="bold"><var class="pv">numeric-expression</var></dt>
|
||
|
<dd>The argument values must be any built-in numeric data type.
|
||
|
</dd>
|
||
|
</dl>
|
||
|
<p>The data type of the result is double-precision floating point.</p>
|
||
|
<p>The function is applied to the set of values derived from the argument
|
||
|
values by the elimination of null values. If DISTINCT is specified, duplicate
|
||
|
values are eliminated.</p>
|
||
|
<p> The result can be null. If the function is applied to the empty set, the
|
||
|
result is a null value. Otherwise, the result is the standard deviation of
|
||
|
the values in the set.</p>
|
||
|
<p>The order in which the values are added is undefined, but every intermediate
|
||
|
result must be within the range of the result data type.</p>
|
||
|
<a name="wq494"></a>
|
||
|
<h4 id="wq494">Note</h4>
|
||
|
<p><span class="bold">Syntax alternatives:</span> STDEV_POP should be used for
|
||
|
conformance to the SQL 1999 standard.</p>
|
||
|
<a name="wq495"></a>
|
||
|
<h4 id="wq495">Example</h4>
|
||
|
<ul>
|
||
|
<li>Using the EMPLOYEE table, set the host variable DEV (double-precision
|
||
|
floating point) to the standard deviation of the salaries for those employees
|
||
|
in department A00.
|
||
|
<pre class="xmp"> <span class="bold">SELECT STDDEV_POP</span>(SALARY)
|
||
|
<span class="bold">INTO</span> :DEV
|
||
|
<span class="bold">FROM</span> EMPLOYEE
|
||
|
<span class="bold">WHERE</span> WORKDEPT = 'A00';</pre>Results in DEV being
|
||
|
set to approximately 9742.43.</li></ul>
|
||
|
<a name="colstddevsamp"></a>
|
||
|
<h3 id="colstddevsamp"><a href="rbafzmst02.htm#ToC_330">STDDEV_SAMP</a></h3><a id="idx824" name="idx824"></a><a id="idx825" name="idx825"></a><a id="idx826" name="idx826"></a><a id="idx827" name="idx827"></a>
|
||
|
<a href="rbafzmstcolfunc.htm#synstddev1"><img src="c.gif" alt="Click to skip syntax diagram" /></a>
|
||
|
<a name="wq496"></a>
|
||
|
<div class="fignone" id="wq496">
|
||
|
<pre class="cgraphic"><span><img src="c.gif" alt="Read syntax diagram" longdesc="rbafzmstsyn44.htm"
|
||
|
border="0" /></span><a href="#skipsyn-43"><img src="c.gif" alt="Skip visual syntax diagram"
|
||
|
border="0" /></a> .-ALL------.
|
||
|
>>-STDDEV_SAMP--(--+----------+--<span class="italic">numeric-expression</span>--)---------><
|
||
|
'-DISTINCT-'
|
||
|
|
||
|
</pre>
|
||
|
<a name="skipsyn-43" id="skipsyn-43"></a></div>
|
||
|
<a name="synstddev1"></a>
|
||
|
<p id="synstddev1">The STDDEV_SAMP function returns the sample standard deviation
|
||
|
(/n-1) of a set of numbers. The formula used to calculate the sample standard
|
||
|
deviation is: </p>
|
||
|
<pre class="xmp">STDDEV_SAMP = SQRT(VAR_SAMP)</pre><p class="indatacontent"> where SQRT(VAR_SAMP)
|
||
|
is the square root of the sample variance.</p>
|
||
|
<dl class="parml">
|
||
|
<dt class="bold"><var class="pv">numeric-expression</var></dt>
|
||
|
<dd>The argument values must be any built-in numeric data type.
|
||
|
</dd>
|
||
|
</dl>
|
||
|
<p>The data type of the result is double-precision floating point.</p>
|
||
|
<p>The function is applied to the set of values derived from the argument
|
||
|
values by the elimination of null values. If DISTINCT is specified, duplicate
|
||
|
values are eliminated.</p>
|
||
|
<p> The result can be null. If the function is applied to the empty set or
|
||
|
a set with only one row, the result is a null value. Otherwise, the result
|
||
|
is the standard deviation of the values in the set.</p>
|
||
|
<p>The order in which the values are added is undefined, but every intermediate
|
||
|
result must be within the range of the result data type.</p>
|
||
|
<a name="wq497"></a>
|
||
|
<h4 id="wq497">Example</h4>
|
||
|
<ul>
|
||
|
<li>Using the EMPLOYEE table, set the host variable DEV (double-precision
|
||
|
floating point) to the sample standard deviation of the salaries for those
|
||
|
employees in department A00.
|
||
|
<pre class="xmp"> <span class="bold">SELECT STDDEV_SAMP</span>(SALARY)
|
||
|
<span class="bold">INTO</span> :DEV
|
||
|
<span class="bold">FROM</span> EMPLOYEE
|
||
|
<span class="bold">WHERE</span> WORKDEPT = 'A00';</pre>Results in DEV being
|
||
|
set to approximately 10892.37.</li></ul>
|
||
|
<a name="colsum"></a>
|
||
|
<h3 id="colsum"><a href="rbafzmst02.htm#ToC_332">SUM</a></h3><a id="idx828" name="idx828"></a><a id="idx829" name="idx829"></a><a id="idx830" name="idx830"></a><a id="idx831" name="idx831"></a>
|
||
|
<a href="rbafzmstcolfunc.htm#synsum"><img src="c.gif" alt="Click to skip syntax diagram" /></a>
|
||
|
<a name="wq498"></a>
|
||
|
<div class="fignone" id="wq498">
|
||
|
<pre class="cgraphic"><span><img src="c.gif" alt="Read syntax diagram" longdesc="rbafzmstsyn45.htm"
|
||
|
border="0" /></span><a href="#skipsyn-44"><img src="c.gif" alt="Skip visual syntax diagram"
|
||
|
border="0" /></a> .-ALL------.
|
||
|
>>-SUM--(--+----------+--<span class="italic">numeric-expression</span>--)-----------------><
|
||
|
'-DISTINCT-'
|
||
|
|
||
|
</pre>
|
||
|
<a name="skipsyn-44" id="skipsyn-44"></a></div>
|
||
|
<a name="synsum"></a>
|
||
|
<p id="synsum">The SUM function returns the sum of a set of numbers.</p>
|
||
|
<dl class="parml">
|
||
|
<dt class="bold"><var class="pv">numeric-expression</var></dt>
|
||
|
<dd>The argument values must be any built-in numeric data type.
|
||
|
</dd>
|
||
|
</dl>
|
||
|
<p>The data type of the result is the same as the data type of the argument
|
||
|
values except that the result is: </p>
|
||
|
<ul>
|
||
|
<li>A double-precision floating point if the argument values are single-precision
|
||
|
floating point</li>
|
||
|
<li>A large integer if the argument values are small integers</li>
|
||
|
<li>A decimal with precision <span class="italic">mp</span> and scale <var class="pv">s</var> if
|
||
|
the argument values are decimal or nonzero scale binary numbers with precision <var class="pv">p</var> and scale <var class="pv">s</var>.</li></ul><p class="indatacontent">For information on the values of <span class="italic">p</span>, <span class="italic">s</span>, and <span class="italic">mp</span>, see <a href="rbafzmstch2expr.htm#decarithmetic">Decimal arithmetic in SQL</a>.</p>
|
||
|
<p>The function is applied to the set of values derived from the argument
|
||
|
values by the elimination of null values. If DISTINCT is specified, duplicate
|
||
|
values are eliminated.</p>
|
||
|
<p>The result can be null. If the function is applied to the empty set, the
|
||
|
result is a null value. Otherwise, the result is the sum of the values in
|
||
|
the set.</p>
|
||
|
<p>The order in which the values are added is undefined, but every intermediate
|
||
|
result must be within the range of the result data type.</p>
|
||
|
<a name="wq499"></a>
|
||
|
<h4 id="wq499">Example</h4>
|
||
|
<ul>
|
||
|
<li>Using the EMPLOYEE table, set the host variable JOB_BONUS
|
||
|
(DECIMAL(9,2)) to the total bonus (BONUS) paid to clerks (JOB='CLERK').
|
||
|
<pre class="xmp"> <span class="bold">SELECT SUM(</span>BONUS<span class="bold">)</span>
|
||
|
<span class="bold">INTO</span> :JOB_BONUS
|
||
|
<span class="bold">FROM</span> EMPLOYEE
|
||
|
<span class="bold">WHERE</span> JOB = 'CLERK'</pre> Results in JOB_BONUS
|
||
|
being set to 4000.</li></ul>
|
||
|
<a name="colvar"></a>
|
||
|
<h3 id="colvar"><a href="rbafzmst02.htm#ToC_334">VAR_POP or VARIANCE or VAR</a></h3><a id="idx832" name="idx832"></a><a id="idx833" name="idx833"></a><a id="idx834" name="idx834"></a><a id="idx835" name="idx835"></a><a id="idx836" name="idx836"></a><a id="idx837" name="idx837"></a><a id="idx838" name="idx838"></a><a id="idx839" name="idx839"></a><a id="idx840" name="idx840"></a><a id="idx841" name="idx841"></a><a id="idx842" name="idx842"></a><a id="idx843" name="idx843"></a>
|
||
|
<a href="rbafzmstcolfunc.htm#synvar"><img src="c.gif" alt="Click to skip syntax diagram" /></a>
|
||
|
<a name="wq500"></a>
|
||
|
<div class="fignone" id="wq500">
|
||
|
<pre class="cgraphic"><span><img src="c.gif" alt="Read syntax diagram" longdesc="rbafzmstsyn46.htm"
|
||
|
border="0" /></span><a href="#skipsyn-45"><img src="c.gif" alt="Skip visual syntax diagram"
|
||
|
border="0" /></a> .-ALL------.
|
||
|
>>-+-VAR_POP--+--(--+----------+--<span class="italic">numeric-expression</span>--)--------><
|
||
|
+-VARIANCE-+ '-DISTINCT-'
|
||
|
'-VAR------'
|
||
|
|
||
|
</pre>
|
||
|
<a name="skipsyn-45" id="skipsyn-45"></a></div>
|
||
|
<a name="synvar"></a>
|
||
|
<p id="synvar">The VAR_POP function returns the biased variance (/n) of a
|
||
|
set of numbers. The formula used to calculate the biased variance is: </p>
|
||
|
<pre class="xmp"> VAR_POP = SUM(X**2)/COUNT(X) - (SUM(X)/COUNT(X))**2</pre>
|
||
|
<dl class="parml">
|
||
|
<dt class="bold"><var class="pv">numeric-expression</var></dt>
|
||
|
<dd>The argument values must be any built-in numeric data type.
|
||
|
</dd>
|
||
|
</dl>
|
||
|
<p>The data type of the result is double-precision floating point.</p>
|
||
|
<p>The function is applied to the set of values derived from the argument
|
||
|
values by the elimination of null values. If DISTINCT is specified, duplicate
|
||
|
values are eliminated.</p>
|
||
|
<p>The result can be null. If the function is applied to the empty set, the
|
||
|
result is a null value. Otherwise, the result is the variance of the values
|
||
|
in the set.</p>
|
||
|
<p>The order in which the values are added is undefined, but every intermediate
|
||
|
result must be within the range of the result data type.</p>
|
||
|
<a name="wq501"></a>
|
||
|
<h4 id="wq501">Note</h4>
|
||
|
<p><span class="bold">Syntax alternatives:</span> VAR_POP should be used for conformance
|
||
|
to the SQL 1999 standard.</p>
|
||
|
<a name="wq502"></a>
|
||
|
<h4 id="wq502">Example</h4>
|
||
|
<ul>
|
||
|
<li>Using the EMPLOYEE table, set the host variable VARNCE (double-precision
|
||
|
floating point) to the variance of the salaries for those employees in department
|
||
|
A00.
|
||
|
<pre class="xmp"> <span class="bold">SELECT VAR_POP</span>(SALARY)
|
||
|
<span class="bold">INTO</span> :VARNCE
|
||
|
<span class="bold">FROM</span> EMPLOYEE
|
||
|
<span class="bold">WHERE</span> WORKDEPT = 'A00';</pre>Results
|
||
|
in VARNCE being set to approximately 94 915 000.</li></ul>
|
||
|
<a name="colvarsamp"></a>
|
||
|
<h3 id="colvarsamp"><a href="rbafzmst02.htm#ToC_337">VARIANCE_SAMP or VAR_SAMP</a></h3><a id="idx844" name="idx844"></a><a id="idx845" name="idx845"></a><a id="idx846" name="idx846"></a><a id="idx847" name="idx847"></a><a id="idx848" name="idx848"></a><a id="idx849" name="idx849"></a><a id="idx850" name="idx850"></a><a id="idx851" name="idx851"></a><a id="idx852" name="idx852"></a><a id="idx853" name="idx853"></a>
|
||
|
<a href="rbafzmstcolfunc.htm#synvar1"><img src="c.gif" alt="Click to skip syntax diagram" /></a>
|
||
|
<a name="wq503"></a>
|
||
|
<div class="fignone" id="wq503">
|
||
|
<pre class="cgraphic"><span><img src="c.gif" alt="Read syntax diagram" longdesc="rbafzmstsyn47.htm"
|
||
|
border="0" /></span><a href="#skipsyn-46"><img src="c.gif" alt="Skip visual syntax diagram"
|
||
|
border="0" /></a> .-ALL------.
|
||
|
>>-+-VAR_SAMP------+--(--+----------+--<span class="italic">numeric-expression</span>--)---><
|
||
|
'-VARIANCE_SAMP-' '-DISTINCT-'
|
||
|
|
||
|
</pre>
|
||
|
<a name="skipsyn-46" id="skipsyn-46"></a></div>
|
||
|
<a name="synvar1"></a>
|
||
|
<p id="synvar1">The VAR_SAMP function returns the sample variance (/n-1) of
|
||
|
a set of numbers. The formula used to calculate the sample variance is: </p>
|
||
|
<pre class="xmp"> VAR_SAMP = (SUM(X**2) - ((SUM(X)**2) / (COUNT(*)))) / (COUNT(*) - 1)</pre>
|
||
|
<dl class="parml">
|
||
|
<dt class="bold"><var class="pv">numeric-expression</var></dt>
|
||
|
<dd>The argument values must be any built-in numeric data type.
|
||
|
</dd>
|
||
|
</dl>
|
||
|
<p>The data type of the result is double-precision floating point.</p>
|
||
|
<p>The function is applied to the set of values derived from the argument
|
||
|
values by the elimination of null values. If DISTINCT is specified, duplicate
|
||
|
values are eliminated.</p>
|
||
|
<p>The result can be null. If the function is applied to the empty
|
||
|
set or a set with only one row, the result is a null value. Otherwise, the
|
||
|
result is the variance of the values in the set.</p>
|
||
|
<p>The order in which the values are added is undefined, but every intermediate
|
||
|
result must be within the range of the result data type.</p>
|
||
|
<a name="wq504"></a>
|
||
|
<h4 id="wq504">Note</h4>
|
||
|
<p><span class="bold">Syntax alternatives:</span> VAR_SAMP should be used for
|
||
|
conformance to the SQL 1999 standard.</p>
|
||
|
<a name="wq505"></a>
|
||
|
<h4 id="wq505">Example</h4>
|
||
|
<ul>
|
||
|
<li>Using the EMPLOYEE table, set the host variable VARNCE (double-precision
|
||
|
floating point) to the sample variance of the salaries for those employees
|
||
|
in department A00.
|
||
|
<pre class="xmp"> <span class="bold">SELECT VAR_SAMP</span>(SALARY)
|
||
|
<span class="bold">INTO</span> :VARNCE
|
||
|
<span class="bold">FROM</span> EMPLOYEE
|
||
|
<span class="bold">WHERE</span> WORKDEPT = 'A00';</pre>Results
|
||
|
in VARNCE being set to approximately 1 186 437 500.</li></ul>
|
||
|
<hr /><br />
|
||
|
[ <a href="#Top_Of_Page">Top of Page</a> | <a href="rbafzmstch2func.htm">Previous Page</a> | <a href="rbafzmstscale.htm">Next Page</a> | <a href="rbafzmst02.htm#wq1">Contents</a> |
|
||
|
<a href="rbafzmstindex.htm#index">Index</a> ]
|
||
|
|
||
|
<a id="Bot_Of_Page" name="Bot_Of_Page"></a>
|
||
|
</body>
|
||
|
</html>
|