doc-exports/docs/dli/sqlreference/dli_08_0177.html
Su, Xiaomeng 04d4597cf3 dli_sqlreference_0511_version
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>
2023-11-02 14:34:08 +00:00

37 lines
5.7 KiB
HTML

<a name="dli_08_0177"></a><a name="dli_08_0177"></a>
<h1 class="topictitle1">Subquery Nested by HAVING</h1>
<div id="body8662426"><div class="section" id="dli_08_0177__en-us_topic_0093946801_s56fcb4e767cf4c84adeb0bada219b425"><h4 class="sectiontitle">Function</h4><p id="dli_08_0177__en-us_topic_0093946801_a70357ea543514c5aaf9272d71072c962">This statement is used to embed a subquery in the HAVING clause. The subquery result is used as a part of the HAVING clause.</p>
</div>
<div class="section" id="dli_08_0177__en-us_topic_0093946801_s36489588dff74b60b688df50516f7e65"><h4 class="sectiontitle">Syntax</h4><div class="codecoloring" codetype="Sql" id="dli_08_0177__screen1471117191417"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
<span class="normal">2</span>
<span class="normal">3</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="p">[</span><span class="k">ALL</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="k">DISTINCT</span><span class="p">]</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">groupby_expression</span>
<span class="w"> </span><span class="k">HAVING</span><span class="w"> </span><span class="n">aggregate_func</span><span class="p">(</span><span class="n">col_name</span><span class="p">)</span><span class="w"> </span><span class="k">operator</span><span class="w"> </span><span class="p">(</span><span class="n">sub_query</span><span class="p">);</span>
</pre></div></td></tr></table></div>
</div>
</div>
<div class="section" id="dli_08_0177__en-us_topic_0093946801_s1103899f2e864d669b626f060512da51"><h4 class="sectiontitle">Keyword</h4><ul id="dli_08_0177__en-us_topic_0093946801_uad6ec7edf7764d96a8ac3eaa9b1b06c8"><li id="dli_08_0177__en-us_topic_0093946801_l3f323f0e1ed846b2b28d5eac548c2323">All is used to return repeated rows. By default, all repeated rows are returned. It is followed by asterisks (*) only. Otherwise, an error will occur.</li><li id="dli_08_0177__en-us_topic_0093946801_l9631a98db3454577b0b4ca151b1f4bb9">DISTINCT is used to remove the repeated line from the result.</li></ul>
<ul id="dli_08_0177__en-us_topic_0093946801_u15d5e4f3f03f4eb6bcdcac2a30aa5c17"><li id="dli_08_0177__en-us_topic_0093946801_l1743c3337a52484cbaaca74d88d75e22">The <strong id="dli_08_0177__b942163220328">groupby_expression</strong> can contain a single field or multiple fields, and also can call aggregate functions or string functions. </li><li id="dli_08_0177__en-us_topic_0093946801_lb5c2c1f6d4f442f598911773abc6c893">The operator includes the equation and inequation operators, and IN and NOT IN operators.</li></ul>
</div>
<div class="section" id="dli_08_0177__en-us_topic_0093946801_s8c6ee38066154c1681e4ed821c997459"><h4 class="sectiontitle">Precautions</h4><ul id="dli_08_0177__en-us_topic_0093946801_u0414e5ef59d941b19e24f3cad302fc3c"><li id="dli_08_0177__en-us_topic_0093946801_l4e2d7aab0a254602a71252d70efb70f7">The to-be-queried table must exist. If this statement is used to query a table that does not exist, an error is reported. </li><li id="dli_08_0177__en-us_topic_0093946801_lc9dfe60af6a84401a21f032cea427fb0">The sequence of <strong id="dli_08_0177__b1050814993218">sub_query</strong> and the aggregate function cannot be changed. </li></ul>
</div>
<div class="section" id="dli_08_0177__en-us_topic_0093946801_s2a8900170872472999b4cbf68d5812a9"><h4 class="sectiontitle">Example</h4><p id="dli_08_0177__en-us_topic_0093946801_a95bd20e7d09a480ebcafae3c21237462">To group the <strong id="dli_08_0177__b1544614590326">student_info</strong> table according to the name field, count the records of each group, and return the number of records in which the name fields in the <strong id="dli_08_0177__b10943114163318">student_info</strong> table equal to the name fields in the <strong id="dli_08_0177__b1950912913333">course_info</strong> table if the two tables have the same number of records, run the following statement:</p>
<div class="codecoloring" codetype="Sql" id="dli_08_0177__screen14179164114219"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
<span class="normal">2</span>
<span class="normal">3</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">student_info</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">name</span>
<span class="w"> </span><span class="k">HAVING</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="o">=</span><span class="w"> </span><span class="p">(</span><span class="k">SELECT</span><span class="w"> </span><span class="k">count</span><span class="p">(</span><span class="o">*</span><span class="p">)</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">course_info</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_0174.html">Subquery</a></div>
</div>
</div>