forked from docs/doc-exports
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>
33 lines
4.5 KiB
HTML
33 lines
4.5 KiB
HTML
<a name="dli_08_0160"></a><a name="dli_08_0160"></a>
|
|
|
|
<h1 class="topictitle1">Column-Based GROUP BY</h1>
|
|
<div id="body8662426"><div class="section" id="dli_08_0160__en-us_topic_0093946941_s1167cb71e67c49a4ba57774640e6cddd"><h4 class="sectiontitle">Function</h4><p id="dli_08_0160__en-us_topic_0093946941_af79ff586ab754c459453d4943d5a8314">This statement is used to group a table based on columns. </p>
|
|
</div>
|
|
<div class="section" id="dli_08_0160__en-us_topic_0093946941_sc598c024b6b64abbba19dae59f677493"><h4 class="sectiontitle">Syntax</h4><div class="codecoloring" codetype="Sql" id="dli_08_0160__screen1345921114426"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
|
|
<span class="normal">2</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="n">attr_expr_list</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">table_reference</span>
|
|
<span class="w"> </span><span class="k">GROUP</span><span class="w"> </span><span class="k">BY</span><span class="w"> </span><span class="n">col_name_list</span><span class="p">;</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
</div>
|
|
<div class="section" id="dli_08_0160__en-us_topic_0093946941_scd3c1fa486dd465081854b5b833e927e"><h4 class="sectiontitle">Keywords</h4><p id="dli_08_0160__en-us_topic_0093946941_af798b4657bef470cb3eee964deeb8866">Column-based GROUP BY can be categorized into single-column GROUP BY and multi-column GROUP BY.</p>
|
|
<ul id="dli_08_0160__en-us_topic_0093946941_u5cfcc91bac394364b38762f5c08bf61b"><li id="dli_08_0160__en-us_topic_0093946941_l94e9612fdf274f179caf7540eedf6b34">Single-column GROUP BY indicates that the GROUP BY clause contains only one column. The fields in <strong id="dli_08_0160__b1654961883212">col_name_list</strong> must exist in <strong id="dli_08_0160__b18532122315326">attr_expr_list</strong>. The aggregate function, <strong id="dli_08_0160__b2827205216328">count()</strong> and <strong id="dli_08_0160__b14627358193220">sum()</strong> for example, is supported in <strong id="dli_08_0160__b1867620318333">attr_expr_list</strong>. The aggregate function can contain other fields.</li><li id="dli_08_0160__en-us_topic_0093946941_l59fa1427450745f5935f91f86446397b">Multi-column GROUP BY indicates that there is more than one column in the GROUP BY clause. The query statement is grouped according to all the fields in the GROUP BY clause. The records with the same fields are put in the same group. Similarly, the fields in the GROUP BY clause must be in the fields in <strong id="dli_08_0160__b461324723318">attr_expr_list</strong>. The <strong id="dli_08_0160__b173617355345">attr_expr_list</strong> field can also use the aggregate function.</li></ul>
|
|
</div>
|
|
<div class="section" id="dli_08_0160__en-us_topic_0093946941_s5593979557b94dc3b677c3e53faaaea7"><h4 class="sectiontitle">Precautions</h4><p id="dli_08_0160__en-us_topic_0093946941_a0590c76cc2ad4b9e856702033b0c0df3">The to-be-grouped table must exist. Otherwise, an error is reported.</p>
|
|
</div>
|
|
<div class="section" id="dli_08_0160__en-us_topic_0093946941_s8c4e91579ac74cafaa4a726b489a9e82"><h4 class="sectiontitle">Example</h4><p id="dli_08_0160__en-us_topic_0093946941_a3f6e453fd3064c84993daa71b1144355">Group the <strong id="dli_08_0160__b84235270694141">student</strong> table according to the score and name fields and return the grouping results.</p>
|
|
<div class="codecoloring" codetype="Sql" id="dli_08_0160__screen34801547154216"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
|
|
<span class="normal">2</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="n">score</span><span class="p">,</span><span class="w"> </span><span class="k">count</span><span class="p">(</span><span class="n">name</span><span class="p">)</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">student</span>
|
|
<span class="w"> </span><span class="k">GROUP</span><span class="w"> </span><span class="k">BY</span><span class="w"> </span><span class="n">score</span><span class="p">,</span><span class="n">name</span><span class="p">;</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="dli_08_0159.html">Grouping</a></div>
|
|
</div>
|
|
</div>
|
|
|