doc-exports/docs/dli/sqlreference/dli_08_0190.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

58 lines
13 KiB
HTML

<a name="dli_08_0190"></a><a name="dli_08_0190"></a>
<h1 class="topictitle1">OVER Clause</h1>
<div id="body8662426"><div class="section" id="dli_08_0190__en-us_topic_0093946879_s980a55e3a766496db2cb450e0c2e79ce"><h4 class="sectiontitle">Function</h4><p id="dli_08_0190__en-us_topic_0093946879_a6e73e82da07e484d96317d2522c0fe3e">This statement is used together with the window function. The OVER statement is used to group data and sort the data within the group. The window function is used to generate serial numbers for values within the group.</p>
</div>
<div class="section" id="dli_08_0190__en-us_topic_0093946879_s31ccad8fbbaf4a3e9280363849ea80b4"><h4 class="sectiontitle">Syntax</h4><div class="codecoloring" codetype="Sql" id="dli_08_0190__screen162361734104717"><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>
<span class="normal">4</span>
<span class="normal">5</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="n">window_func</span><span class="p">(</span><span class="n">args</span><span class="p">)</span><span class="w"> </span><span class="n">OVER</span>
<span class="w"> </span><span class="p">([</span><span class="n">PARTITION</span><span class="w"> </span><span class="k">BY</span><span class="w"> </span><span class="n">col_name</span><span class="p">,</span><span class="w"> </span><span class="n">col_name</span><span class="p">,</span><span class="w"> </span><span class="p">...]</span>
<span class="w"> </span><span class="p">[</span><span class="k">ORDER</span><span class="w"> </span><span class="k">BY</span><span class="w"> </span><span class="n">col_name</span><span class="p">,</span><span class="w"> </span><span class="n">col_name</span><span class="p">,</span><span class="w"> </span><span class="p">...]</span>
<span class="w"> </span><span class="p">[</span><span class="k">ROWS</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">RANGE</span><span class="w"> </span><span class="k">BETWEEN</span><span class="w"> </span><span class="p">(</span><span class="k">CURRENT</span><span class="w"> </span><span class="k">ROW</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="p">(</span><span class="n">UNBOUNDED</span><span class="w"> </span><span class="o">|</span><span class="p">[</span><span class="n">num</span><span class="p">])</span><span class="w"> </span><span class="n">PRECEDING</span><span class="p">)</span>
<span class="w"> </span><span class="k">AND</span><span class="w"> </span><span class="p">(</span><span class="k">CURRENT</span><span class="w"> </span><span class="k">ROW</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="p">(</span><span class="w"> </span><span class="n">UNBOUNDED</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="p">[</span><span class="n">num</span><span class="p">])</span><span class="w"> </span><span class="n">FOLLOWING</span><span class="p">)]);</span>
</pre></div></td></tr></table></div>
</div>
</div>
<div class="section" id="dli_08_0190__en-us_topic_0093946879_s7e376262717143e8bfc042f1b688caaa"><h4 class="sectiontitle">Keywords</h4><ul id="dli_08_0190__en-us_topic_0093946879_u310ab6c1c35b4bda9f77baadf8a6de60"><li id="dli_08_0190__en-us_topic_0093946879_l83edab14d69a44029f362585ac3f641e">PARTITION BY: used to partition a table with one or multiple fields. Similar to GROUP BY, PARTITION BY is used to partition table by fields and each partition is a window. The window function can apply to the entire table or specific partitions. A maximum of 7,000 partitions can be created in a single table.</li><li id="dli_08_0190__en-us_topic_0093946879_l49466434b1684d63b99beae5d483fec6">ORDER BY: used to specify the order for the window function to obtain the value. ORDER BY can be used to sort table with one or multiple fields. The sorting order can be ascending (specified by <strong id="dli_08_0190__en-us_topic_0093946879_b842352706154824">ASC</strong>) or descending (specified by <strong id="dli_08_0190__en-us_topic_0093946879_b842352706154830">DESC</strong>). The window is specified by WINDOW. If the window is not specified, the default window is ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. In other words, the window starts from the head of the table or partition (if PARTITION BY is used in the OVER clause) to the current row.</li><li id="dli_08_0190__en-us_topic_0093946879_lc0433b69deb047468236bde3a158e867">WINDOW: used to define the window by specifying a range of rows.</li><li id="dli_08_0190__en-us_topic_0093946879_l8114177a5efd45ca9a57e5334ab8033f">CURRENT ROW: indicates the current row.</li><li id="dli_08_0190__en-us_topic_0093946879_l40902142152d4571bcaa2a12eac38c70">num PRECEDING: used to specify the start of the defined window. The window starts from the num row precedes the current row.</li><li id="dli_08_0190__en-us_topic_0093946879_lee53486fe800400cb6daa36355279f37">UNBOUNDED PRECEDING: used to indicate that there is no start of the window.</li><li id="dli_08_0190__en-us_topic_0093946879_l1ee9f63fe7bf4c4e8c5591e72a19ad17">num FOLLOWING: used to specify the end of the defined window. The window ends from the num row following the current row.</li><li id="dli_08_0190__en-us_topic_0093946879_l60779e7f7757455eb528778356ae9c95">UNBOUNDED FOLLOWING: used to indicate that there is no end of the window.</li><li id="dli_08_0190__en-us_topic_0093946879_l4bbceacdc16e4a1d8da804206096e12d">The differences between ROWS BETWEEN... and RANGE BETWEEN... are as follows:<ul id="dli_08_0190__en-us_topic_0093946879_uff73c10094af427689c9cafea46f2d73"><li id="dli_08_0190__en-us_topic_0093946879_ldcb9e1c66e334f18930e85e3958280a8">ROWS refers to the physical window. After the data is sorted, the physical window starts at the <em id="dli_08_0190__en-us_topic_0093946879_i842352697155041">n</em>th row in front of the current row and ends at the <em id="dli_08_0190__en-us_topic_0093946879_i842352697155051">m</em>th row following the current row.</li><li id="dli_08_0190__en-us_topic_0093946879_la73e229da3ff4d89801fcb4e83495335">RANGE refers to the logic window. The column of the logic window is determined by the values rather than the location of rows.</li></ul>
</li><li id="dli_08_0190__en-us_topic_0093946879_l178ba93486d84b18a368ac29849af251">The scenarios of the window are as follows:<ul id="dli_08_0190__en-us_topic_0093946879_u1be4993bce0f423d916fba0555335e3e"><li id="dli_08_0190__en-us_topic_0093946879_l6bf04def5c0b4fb7870fbef7f7508081">The window only contains the current row.<div class="codecoloring" codetype="Sql" id="dli_08_0190__screen999831414916"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">ROWS</span><span class="w"> </span><span class="k">BETWEEN</span><span class="w"> </span><span class="k">CURRENT</span><span class="w"> </span><span class="k">ROW</span><span class="w"> </span><span class="k">AND</span><span class="w"> </span><span class="k">CURRENT</span><span class="w"> </span><span class="k">ROW</span>
</pre></div></td></tr></table></div>
</div>
</li><li id="dli_08_0190__en-us_topic_0093946879_l6cd09a1019ae4bc6ae4c8701107e803e">The window starts from three rows precede the current row and ends at the fifth row follows the current row.<div class="codecoloring" codetype="Sql" id="dli_08_0190__screen06081297499"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">ROWS</span><span class="w"> </span><span class="k">BETWEEN</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="n">PRECEDING</span><span class="w"> </span><span class="k">AND</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="n">FOLLOWING</span>
</pre></div></td></tr></table></div>
</div>
</li><li id="dli_08_0190__en-us_topic_0093946879_l268036adbd4c472697af3a05bb34e436">The window starts from the beginning of the table or partition and ends at the current row.<div class="codecoloring" codetype="Sql" id="dli_08_0190__screen6874246204917"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">ROWS</span><span class="w"> </span><span class="k">BETWEEN</span><span class="w"> </span><span class="n">UNBOUNDED</span><span class="w"> </span><span class="n">PRECEDING</span><span class="w"> </span><span class="k">AND</span><span class="w"> </span><span class="k">CURRENT</span><span class="w"> </span><span class="k">ROW</span>
</pre></div></td></tr></table></div>
</div>
</li><li id="dli_08_0190__en-us_topic_0093946879_l3ce49db7f4994565a297a6082eac2d74">The window starts from the current window and ends at the end of the table or partition.<div class="codecoloring" codetype="Sql" id="dli_08_0190__screen523616375016"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">ROWS</span><span class="w"> </span><span class="k">BETWEEN</span><span class="w"> </span><span class="k">CURRENT</span><span class="w"> </span><span class="k">ROW</span><span class="w"> </span><span class="k">AND</span><span class="w"> </span><span class="n">UNBOUNDED</span><span class="w"> </span><span class="n">FOLLOWING</span>
</pre></div></td></tr></table></div>
</div>
</li><li id="dli_08_0190__en-us_topic_0093946879_l49dc85191e734a6e9bebb37320c73d44">The window starts from the beginning of the table or partition and ends at the end of the table or partition.<div class="codecoloring" codetype="Sql" id="dli_08_0190__screen1410617179501"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">ROWS</span><span class="w"> </span><span class="k">BETWEEN</span><span class="w"> </span><span class="n">UNBOUNDED</span><span class="w"> </span><span class="n">PRECEDING</span><span class="w"> </span><span class="k">AND</span><span class="w"> </span><span class="n">UNBOUNDED</span><span class="w"> </span><span class="n">FOLLOWING</span>
</pre></div></td></tr></table></div>
</div>
</li></ul>
</li></ul>
</div>
<div class="section" id="dli_08_0190__en-us_topic_0093946879_s4623faf311b44f25b436cd84d2f7833a"><h4 class="sectiontitle">Precautions</h4><p id="dli_08_0190__en-us_topic_0093946879_a8c398417109c4f10ae5d63e353167ec2">The three options of the OVER clause are PARTITION BY, ORDER BY, and WINDOW. They are optional and can be used together. If the OVER clause is empty, the window is the entire table.</p>
</div>
<div class="section" id="dli_08_0190__en-us_topic_0093946879_s41ec47c3d8f44ab5957a8838671ad180"><h4 class="sectiontitle">Example</h4><p id="dli_08_0190__en-us_topic_0093946879_aea6431b036474a86b8f11924b532c840">To start the window from the beginning of the table or partition and end the window at the current row, sort the over_test table according to the id field, and return the sorted id fields and corresponding serial numbers, run the following statement:</p>
</div>
<div class="codecoloring" codetype="Sql" id="dli_08_0190__screen1016173465017"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="n">id</span><span class="p">,</span><span class="w"> </span><span class="k">count</span><span class="p">(</span><span class="n">id</span><span class="p">)</span><span class="w"> </span><span class="n">OVER</span><span class="w"> </span><span class="p">(</span><span class="k">ORDER</span><span class="w"> </span><span class="k">BY</span><span class="w"> </span><span class="n">id</span><span class="w"> </span><span class="k">ROWS</span><span class="w"> </span><span class="k">BETWEEN</span><span class="w"> </span><span class="n">UNBOUNDED</span><span class="w"> </span><span class="n">PRECEDING</span><span class="w"> </span><span class="k">AND</span><span class="w"> </span><span class="k">CURRENT</span><span class="w"> </span><span class="k">ROW</span><span class="p">)</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">over_test</span><span class="p">;</span>
</pre></div></td></tr></table></div>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="dli_08_0221.html">Spark SQL Syntax Reference</a></div>
</div>
</div>