doc-exports/docs/dli/sqlreference/dli_spark_count.html
Su, Xiaomeng 76a5b1ee83 dli_sqlreference_20240227
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>
2024-03-27 22:02:33 +00:00

64 lines
6.6 KiB
HTML

<a name="dli_spark_count"></a><a name="dli_spark_count"></a>
<h1 class="topictitle1">count</h1>
<div id="body8662426"><p id="dli_spark_count__en-us_topic_0000001655540506_p14343326182513">This function is used to return the number of records.</p>
<div class="section" id="dli_spark_count__en-us_topic_0000001655540506_section45291954203217"><h4 class="sectiontitle">Syntax</h4><pre class="screen" id="dli_spark_count__en-us_topic_0000001655540506_screen19928459112517">count([distinct|all] &lt;colname&gt;)</pre>
</div>
<div class="section" id="dli_spark_count__en-us_topic_0000001655540506_section992014913317"><h4 class="sectiontitle">Parameters</h4>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="dli_spark_count__en-us_topic_0000001655540506_table1829154762513" frame="border" border="1" rules="all"><caption><b>Table 1 </b>Parameters</caption><thead align="left"><tr id="dli_spark_count__en-us_topic_0000001655540506_row8830104792517"><th align="left" class="cellrowborder" valign="top" width="22.759999999999998%" id="mcps1.3.3.2.2.4.1.1"><p id="dli_spark_count__en-us_topic_0000001655540506_p983074711252">Parameter</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="15.920000000000002%" id="mcps1.3.3.2.2.4.1.2"><p id="dli_spark_count__en-us_topic_0000001655540506_p6830124732517">Mandatory</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="61.31999999999999%" id="mcps1.3.3.2.2.4.1.3"><p id="dli_spark_count__en-us_topic_0000001655540506_p08301547132513">Description</p>
</th>
</tr>
</thead>
<tbody><tr id="dli_spark_count__en-us_topic_0000001655540506_row15830184792511"><td class="cellrowborder" valign="top" width="22.759999999999998%" headers="mcps1.3.3.2.2.4.1.1 "><p id="dli_spark_count__en-us_topic_0000001655540506_p10776680287">distinct or all</p>
</td>
<td class="cellrowborder" valign="top" width="15.920000000000002%" headers="mcps1.3.3.2.2.4.1.2 "><p id="dli_spark_count__en-us_topic_0000001655540506_p12830184752518">No</p>
</td>
<td class="cellrowborder" valign="top" width="61.31999999999999%" headers="mcps1.3.3.2.2.4.1.3 "><p id="dli_spark_count__en-us_topic_0000001655540506_p123981226142913">Determines whether duplicate records should be excluded during counting. <strong id="dli_spark_count__en-us_topic_0000001655540506_b26051845192617">all</strong> is used by default, indicating that all records will be included in the count.</p>
<p id="dli_spark_count__en-us_topic_0000001655540506_p9484232112711">If <strong id="dli_spark_count__en-us_topic_0000001655540506_b592818368273">distinct</strong> is specified, only the number of unique values is counted.</p>
</td>
</tr>
<tr id="dli_spark_count__en-us_topic_0000001655540506_row024081612817"><td class="cellrowborder" valign="top" width="22.759999999999998%" headers="mcps1.3.3.2.2.4.1.1 "><p id="dli_spark_count__en-us_topic_0000001655540506_p92401816172810">colname</p>
</td>
<td class="cellrowborder" valign="top" width="15.920000000000002%" headers="mcps1.3.3.2.2.4.1.2 "><p id="dli_spark_count__en-us_topic_0000001655540506_p18240101672819">Yes</p>
</td>
<td class="cellrowborder" valign="top" width="61.31999999999999%" headers="mcps1.3.3.2.2.4.1.3 "><p id="dli_spark_count__en-us_topic_0000001655540506_p492115310298">The column value can be of any type.</p>
<p id="dli_spark_count__en-us_topic_0000001655540506_p112411916152810">The value can be <strong id="dli_spark_count__en-us_topic_0000001655540506_b19928826152813">*</strong>, that is, <strong id="dli_spark_count__en-us_topic_0000001655540506_b2971831192811">count(*)</strong>, indicating that the number of all rows is returned.</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section" id="dli_spark_count__en-us_topic_0000001655540506_section210162513312"><h4 class="sectiontitle">Return Values</h4><p id="dli_spark_count__en-us_topic_0000001655540506_p184124317231">The return value is of the BIGINT type.</p>
<div class="note" id="dli_spark_count__en-us_topic_0000001655540506_note61422099395"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="dli_spark_count__en-us_topic_0000001655540506_p11468110152814">If the value of <strong id="dli_spark_count__en-us_topic_0000001655540506_b1352205819283">colname</strong> is <strong id="dli_spark_count__en-us_topic_0000001655540506_b03537582289">NULL</strong>, the row is not involved in calculation.</p>
</div></div>
</div>
<div class="section" id="dli_spark_count__en-us_topic_0000001655540506_section13277192233920"><h4 class="sectiontitle">Example Code</h4><ul id="dli_spark_count__en-us_topic_0000001655540506_ul551315323411"><li id="dli_spark_count__en-us_topic_0000001655540506_li851313537349">Calculates the total number of records in the warehouse table. An example command is as follows:<pre class="screen" id="dli_spark_count__en-us_topic_0000001655540506_screen189619516297">select count(*) from warehouse;</pre>
<p id="dli_spark_count__en-us_topic_0000001655540506_p14521113310315">The command output is as follows:</p>
<pre class="screen" id="dli_spark_count__en-us_topic_0000001655540506_screen18254245193111">_c0
10 </pre>
</li><li id="dli_spark_count__en-us_topic_0000001655540506_li1249519123517">When used with <strong id="dli_spark_count__en-us_topic_0000001655540506_b103201626203018">group by</strong>, it groups all offerings by warehouse (warehouseId) and calculates the number of offerings in each warehouse (warehouseId). An example command is as follows:<pre class="screen" id="dli_spark_count__en-us_topic_0000001655540506_screen644201752314">select warehouseId, count(*) from warehouse group by warehouseId;</pre>
<p id="dli_spark_count__en-us_topic_0000001655540506_p46681031154710">The command output is as follows:</p>
<pre class="screen" id="dli_spark_count__en-us_topic_0000001655540506_screen2688193762315">warehouseId _c1
city1 6
city2 5
city3 6 </pre>
<p id="dli_spark_count__en-us_topic_0000001655540506_p1080723184714">Example 3: Calculates the number of warehouses through distinct deduplication. An example command is as follows:</p>
<pre class="screen" id="dli_spark_count__en-us_topic_0000001655540506_screen13866959123112">select count(distinct warehouseId) from warehouse;</pre>
<p id="dli_spark_count__en-us_topic_0000001655540506_p683983144719">The command output is as follows:</p>
<pre class="screen" id="dli_spark_count__en-us_topic_0000001655540506_screen766042393217">_c0
3 </pre>
</li></ul>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="dli_08_0474.html">Aggregate Functions</a></div>
</div>
</div>