Files
doc-exports/docs/dli/sqlreference/dli_08_15099.html
Su, Xiaomeng be9eabe464 dli_sqlreference_20250305
Reviewed-by: Pruthi, Vineet <vineet.pruthi@t-systems.com>
Co-authored-by: Su, Xiaomeng <suxiaomeng1@huawei.com>
Co-committed-by: Su, Xiaomeng <suxiaomeng1@huawei.com>
2025-03-25 09:06:21 +00:00

122 lines
13 KiB
HTML

<a name="dli_08_15099"></a><a name="dli_08_15099"></a>
<h1 class="topictitle1">Aggregate Functions</h1>
<div id="body0000001310215809"><p id="dli_08_15099__dli_08_0104_en-us_topic_0060575891_p264654433412">Aggregate functions process all rows as input and produce a single aggregate value as the output.</p>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="dli_08_15099__dli_08_0104_tb90f8c1797b346ca8ac5fcb1eb5d85df" frame="border" border="1" rules="all"><caption><b>Table 1 </b>Aggregate functions</caption><thead align="left"><tr id="dli_08_15099__dli_08_0104_r5790c1fe376b4ab2ace8344bdb73e92d"><th align="left" class="cellrowborder" valign="top" width="38.14%" id="mcps1.3.2.2.3.1.1"><p id="dli_08_15099__dli_08_0104_a0fa16f0341ac40e5ae8eb21eb74d88d1">Function</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="61.86000000000001%" id="mcps1.3.2.2.3.1.2"><p id="dli_08_15099__dli_08_0104_a1e1d30493581472e9ef62cca2f49dcb4">Description</p>
</th>
</tr>
</thead>
<tbody><tr id="dli_08_15099__row162545369308"><td class="cellrowborder" valign="top" width="38.14%" headers="mcps1.3.2.2.3.1.1 "><p id="dli_08_15099__p14846111416202">COUNT([ ALL ] expression | DISTINCT expression1 [, expression2]*)</p>
</td>
<td class="cellrowborder" valign="top" width="61.86000000000001%" headers="mcps1.3.2.2.3.1.2 "><p id="dli_08_15099__p141061020122016">By default or with the keyword <strong id="dli_08_15099__b181974273415">ALL</strong>, returns the number of input rows where the expression is not <strong id="dli_08_15099__b1073155716345">NULL</strong>. Using <strong id="dli_08_15099__b972161033512">DISTINCT</strong> calculates the count after removing duplicates.</p>
</td>
</tr>
<tr id="dli_08_15099__row177961646153015"><td class="cellrowborder" valign="top" width="38.14%" headers="mcps1.3.2.2.3.1.1 "><p id="dli_08_15099__p1192225192018">COUNT(*) | COUNT(1)</p>
</td>
<td class="cellrowborder" valign="top" width="61.86000000000001%" headers="mcps1.3.2.2.3.1.2 "><p id="dli_08_15099__p104102030182015">Returns the number of input rows.</p>
</td>
</tr>
<tr id="dli_08_15099__row14780444113012"><td class="cellrowborder" valign="top" width="38.14%" headers="mcps1.3.2.2.3.1.1 "><p id="dli_08_15099__p13222337192017">AVG([ ALL | DISTINCT ] expression)</p>
</td>
<td class="cellrowborder" valign="top" width="61.86000000000001%" headers="mcps1.3.2.2.3.1.2 "><p id="dli_08_15099__p7422218469">By default or with the keyword <strong id="dli_08_15099__b167808233711">ALL</strong>, returns the average value (arithmetic mean) of the expression across all input rows. Using <strong id="dli_08_15099__b87692268375">DISTINCT</strong> calculates the average after removing duplicates.</p>
</td>
</tr>
<tr id="dli_08_15099__row346434211304"><td class="cellrowborder" valign="top" width="38.14%" headers="mcps1.3.2.2.3.1.1 "><p id="dli_08_15099__p846415423309">SUM([ ALL | DISTINCT ] expression)</p>
</td>
<td class="cellrowborder" valign="top" width="61.86000000000001%" headers="mcps1.3.2.2.3.1.2 "><p id="dli_08_15099__p884445315204">By default or with the keyword <strong id="dli_08_15099__b16975416383">ALL</strong>, returns the sum of the expression across all input rows. Using <strong id="dli_08_15099__b1049819209381">DISTINCT</strong> calculates the sum after removing duplicates.</p>
</td>
</tr>
<tr id="dli_08_15099__row06261340193015"><td class="cellrowborder" valign="top" width="38.14%" headers="mcps1.3.2.2.3.1.1 "><p id="dli_08_15099__p1785018012217">MAX([ ALL | DISTINCT ] expression)</p>
</td>
<td class="cellrowborder" valign="top" width="61.86000000000001%" headers="mcps1.3.2.2.3.1.2 "><p id="dli_08_15099__p3626340103018">By default or with the keyword <strong id="dli_08_15099__b551113519386">ALL</strong>, returns the maximum value of the expression across all input rows. Using <strong id="dli_08_15099__b142914413381">DISTINCT</strong> calculates the maximum after removing duplicates.</p>
</td>
</tr>
<tr id="dli_08_15099__row07021738103020"><td class="cellrowborder" valign="top" width="38.14%" headers="mcps1.3.2.2.3.1.1 "><p id="dli_08_15099__p1651071312112">MIN([ ALL | DISTINCT ] expression )</p>
</td>
<td class="cellrowborder" valign="top" width="61.86000000000001%" headers="mcps1.3.2.2.3.1.2 "><p id="dli_08_15099__p3313191916211">By default or with the keyword <strong id="dli_08_15099__b687175993812">ALL</strong>, returns the minimum value of the expression across all input rows. Using <strong id="dli_08_15099__b1249858193920">DISTINCT</strong> calculates the minimum after removing duplicates.</p>
</td>
</tr>
<tr id="dli_08_15099__row6271720154710"><td class="cellrowborder" valign="top" width="38.14%" headers="mcps1.3.2.2.3.1.1 "><p id="dli_08_15099__p12742017478">STDDEV_POP([ ALL | DISTINCT ] expression)</p>
</td>
<td class="cellrowborder" valign="top" width="61.86000000000001%" headers="mcps1.3.2.2.3.1.2 "><p id="dli_08_15099__p16905193272119">By default or with the keyword <strong id="dli_08_15099__b41152035203910">ALL</strong>, returns the population standard deviation of the expression across all input rows. Using <strong id="dli_08_15099__b1448774217393">DISTINCT</strong> calculates the standard deviation after removing duplicates.</p>
</td>
</tr>
<tr id="dli_08_15099__row1818119179470"><td class="cellrowborder" valign="top" width="38.14%" headers="mcps1.3.2.2.3.1.1 "><p id="dli_08_15099__p859333932119">STDDEV_SAMP([ ALL | DISTINCT ] expression)</p>
</td>
<td class="cellrowborder" valign="top" width="61.86000000000001%" headers="mcps1.3.2.2.3.1.2 "><p id="dli_08_15099__p1086884412112">By default or with the keyword <strong id="dli_08_15099__b20741135913395">ALL</strong>, returns the sample standard deviation of the expression across all input rows. Using <strong id="dli_08_15099__b5594415144011">DISTINCT</strong> calculates the standard deviation after removing duplicates.</p>
</td>
</tr>
<tr id="dli_08_15099__row1511915149476"><td class="cellrowborder" valign="top" width="38.14%" headers="mcps1.3.2.2.3.1.1 "><p id="dli_08_15099__p9615852132116">VAR_POP([ ALL | DISTINCT ] expression)</p>
</td>
<td class="cellrowborder" valign="top" width="61.86000000000001%" headers="mcps1.3.2.2.3.1.2 "><p id="dli_08_15099__p611981484713">By default or with the keyword <strong id="dli_08_15099__b144361457134011">ALL</strong>, returns the population variance (square of the population standard deviation) of the expression across all input rows. Using <strong id="dli_08_15099__b63221688416">DISTINCT</strong> calculates the variance after removing duplicates.</p>
</td>
</tr>
<tr id="dli_08_15099__row668415112472"><td class="cellrowborder" valign="top" width="38.14%" headers="mcps1.3.2.2.3.1.1 "><p id="dli_08_15099__p868431104715">VAR_SAMP([ ALL | DISTINCT ] expression)</p>
</td>
<td class="cellrowborder" valign="top" width="61.86000000000001%" headers="mcps1.3.2.2.3.1.2 "><p id="dli_08_15099__p483471712222">By default or with the keyword <strong id="dli_08_15099__b17137163317414">ALL</strong>, returns the sample variance (square of the sample standard deviation) of the expression across all input rows. Using <strong id="dli_08_15099__b94534794115">DISTINCT</strong> calculates the variance after removing duplicates.</p>
</td>
</tr>
<tr id="dli_08_15099__row175551819488"><td class="cellrowborder" valign="top" width="38.14%" headers="mcps1.3.2.2.3.1.1 "><p id="dli_08_15099__p7415426142218">COLLECT([ ALL | DISTINCT ] expression)</p>
</td>
<td class="cellrowborder" valign="top" width="61.86000000000001%" headers="mcps1.3.2.2.3.1.2 "><p id="dli_08_15099__p10555148174818">By default or with the keyword <strong id="dli_08_15099__b185717516422">ALL</strong>, returns multiple sets of expressions across all input rows. NULL values are ignored. Using <strong id="dli_08_15099__b114573114421">DISTINCT</strong> calculates the sets after removing duplicates.</p>
</td>
</tr>
<tr id="dli_08_15099__row1975646114816"><td class="cellrowborder" valign="top" width="38.14%" headers="mcps1.3.2.2.3.1.1 "><p id="dli_08_15099__p1675612644819">VARIANCE([ ALL | DISTINCT ] expression)</p>
</td>
<td class="cellrowborder" valign="top" width="61.86000000000001%" headers="mcps1.3.2.2.3.1.2 "><p id="dli_08_15099__p175611684811">A synonym for <strong id="dli_08_15099__b380815814217">VAR_SAMP()</strong>.</p>
</td>
</tr>
<tr id="dli_08_15099__row1375974114919"><td class="cellrowborder" valign="top" width="38.14%" headers="mcps1.3.2.2.3.1.1 "><p id="dli_08_15099__p14759104144910">RANK()</p>
</td>
<td class="cellrowborder" valign="top" width="61.86000000000001%" headers="mcps1.3.2.2.3.1.2 "><p id="dli_08_15099__p1475984104910">Returns the rank of a value within a set of values. The result is 1 plus the number of rows preceding or equal to the current row in the ordering of the partition. The rank may not be consecutive in the sequence.</p>
</td>
</tr>
<tr id="dli_08_15099__row18561152194918"><td class="cellrowborder" valign="top" width="38.14%" headers="mcps1.3.2.2.3.1.1 "><p id="dli_08_15099__p9665508236">DENSE_RANK()</p>
</td>
<td class="cellrowborder" valign="top" width="61.86000000000001%" headers="mcps1.3.2.2.3.1.2 "><p id="dli_08_15099__p105616294911">Returns the rank of a value within a set of values. The result is one plus the previously assigned rank value. Unlike the rank function, dense_rank does not leave gaps in the ranking sequence.</p>
</td>
</tr>
<tr id="dli_08_15099__row711175816235"><td class="cellrowborder" valign="top" width="38.14%" headers="mcps1.3.2.2.3.1.1 "><p id="dli_08_15099__p1911114583236">ROW_NUMBER()</p>
</td>
<td class="cellrowborder" valign="top" width="61.86000000000001%" headers="mcps1.3.2.2.3.1.2 "><p id="dli_08_15099__p1511215584233">Assigns a unique sequential number to each row within a window partition based on the ordering of rows by rows. ROW_NUMBER is similar to RANK. ROW_NUMBER numbers all rows sequentially (for example, 1, 2, 3, 4, 5). RANK provides the same sequence value for equal rows (for example, 1, 2, 2, 4, 5).</p>
</td>
</tr>
<tr id="dli_08_15099__row754972162413"><td class="cellrowborder" valign="top" width="38.14%" headers="mcps1.3.2.2.3.1.1 "><p id="dli_08_15099__p1549182142419">LEAD(expression [, offset] [, default])</p>
</td>
<td class="cellrowborder" valign="top" width="61.86000000000001%" headers="mcps1.3.2.2.3.1.2 "><p id="dli_08_15099__p125498211247">Returns the value of the expression at the offset-th row after the current row in the window. The default value of <strong id="dli_08_15099__b1285281594611">offset</strong> is <strong id="dli_08_15099__b17331173011469">1</strong>, and the default value of <strong id="dli_08_15099__b1042482484619">default</strong> is <strong id="dli_08_15099__b1679382612461">NULL</strong>.</p>
</td>
</tr>
<tr id="dli_08_15099__row7947548241"><td class="cellrowborder" valign="top" width="38.14%" headers="mcps1.3.2.2.3.1.1 "><p id="dli_08_15099__p794754102418">LAG(expression [, offset] [, default])</p>
</td>
<td class="cellrowborder" valign="top" width="61.86000000000001%" headers="mcps1.3.2.2.3.1.2 "><p id="dli_08_15099__p1594717472417">Returns the value of the expression at the offset-th row before the current row in the window. The default value of <strong id="dli_08_15099__b4493175934615">offset</strong> is <strong id="dli_08_15099__b149465910462">1</strong>, and the default value of <strong id="dli_08_15099__b1149565924615">default</strong> is <strong id="dli_08_15099__b849655924610">NULL</strong>.</p>
</td>
</tr>
<tr id="dli_08_15099__row136773772417"><td class="cellrowborder" valign="top" width="38.14%" headers="mcps1.3.2.2.3.1.1 "><p id="dli_08_15099__p116771274246">FIRST_VALUE(expression)</p>
</td>
<td class="cellrowborder" valign="top" width="61.86000000000001%" headers="mcps1.3.2.2.3.1.2 "><p id="dli_08_15099__p467737172420">Returns the first value in a set of ordered values.</p>
</td>
</tr>
<tr id="dli_08_15099__row13490705248"><td class="cellrowborder" valign="top" width="38.14%" headers="mcps1.3.2.2.3.1.1 "><p id="dli_08_15099__p749050102417">LAST_VALUE(expression)</p>
</td>
<td class="cellrowborder" valign="top" width="61.86000000000001%" headers="mcps1.3.2.2.3.1.2 "><p id="dli_08_15099__p84904012411">Returns the last value in a set of ordered values.</p>
</td>
</tr>
<tr id="dli_08_15099__row13437249142419"><td class="cellrowborder" valign="top" width="38.14%" headers="mcps1.3.2.2.3.1.1 "><p id="dli_08_15099__p743716495244">LISTAGG(expression [, separator])</p>
</td>
<td class="cellrowborder" valign="top" width="61.86000000000001%" headers="mcps1.3.2.2.3.1.2 "><p id="dli_08_15099__p14437204915245">Concatenates the values of string expressions and places a separator value between them. The default separator value is <strong id="dli_08_15099__b18547551164720">,</strong> if no separator is added at the end of the string.</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="dli_08_15085.html">Built-In Functions</a></div>
</div>
</div>