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

84 lines
6.8 KiB
HTML

<a name="dli_spark_rank"></a><a name="dli_spark_rank"></a>
<h1 class="topictitle1">rank</h1>
<div id="body8662426"><p id="dli_spark_rank__en-us_topic_0000001655540562_p18471163815215">This function is used to return the rank of a value in a set of values. When multiple values share the same rank, the next rank in the sequence is not consecutive.</p>
<div class="section" id="dli_spark_rank__en-us_topic_0000001655540562_section07581471595"><h4 class="sectiontitle">Restrictions</h4><p id="dli_spark_rank__en-us_topic_0000001655540562_p1444411515011">The restrictions on using window functions are as follows:</p>
<ul id="dli_spark_rank__en-us_topic_0000001655540562_ul744465110014"><li id="dli_spark_rank__en-us_topic_0000001655540562_li19444165110019">Window functions can be used only in select statements.</li><li id="dli_spark_rank__en-us_topic_0000001655540562_li3444195117019">Window functions and aggregate functions cannot be nested in window functions.</li><li id="dli_spark_rank__en-us_topic_0000001655540562_li14444145111018">Window functions cannot be used together with aggregate functions of the same level.</li></ul>
</div>
<div class="section" id="dli_spark_rank__en-us_topic_0000001655540562_section45291954203217"><h4 class="sectiontitle">Syntax</h4><pre class="screen" id="dli_spark_rank__en-us_topic_0000001655540562_screen5444155117015">rank() over ([partition_clause] [orderby_clause])</pre>
</div>
<div class="section" id="dli_spark_rank__en-us_topic_0000001655540562_section992014913317"><h4 class="sectiontitle">Parameters</h4>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="dli_spark_rank__en-us_topic_0000001655540562_table1829154762513" frame="border" border="1" rules="all"><caption><b>Table 1 </b>Parameters</caption><thead align="left"><tr id="dli_spark_rank__en-us_topic_0000001655540562_row8830104792517"><th align="left" class="cellrowborder" valign="top" width="22.759999999999998%" id="mcps1.3.4.2.2.4.1.1"><p id="dli_spark_rank__en-us_topic_0000001655540562_p983074711252">Parameter</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="15.920000000000002%" id="mcps1.3.4.2.2.4.1.2"><p id="dli_spark_rank__en-us_topic_0000001655540562_p6830124732517">Mandatory</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="61.31999999999999%" id="mcps1.3.4.2.2.4.1.3"><p id="dli_spark_rank__en-us_topic_0000001655540562_p08301547132513">Description</p>
</th>
</tr>
</thead>
<tbody><tr id="dli_spark_rank__en-us_topic_0000001655540562_row1160161417206"><td class="cellrowborder" valign="top" width="22.759999999999998%" headers="mcps1.3.4.2.2.4.1.1 "><p id="dli_spark_rank__en-us_topic_0000001655540562_p181421114175510">partition_clause</p>
</td>
<td class="cellrowborder" valign="top" width="15.920000000000002%" headers="mcps1.3.4.2.2.4.1.2 "><p id="dli_spark_rank__en-us_topic_0000001655540562_p1214218144555">No</p>
</td>
<td class="cellrowborder" valign="top" width="61.31999999999999%" headers="mcps1.3.4.2.2.4.1.3 "><p id="dli_spark_rank__en-us_topic_0000001655540562_p1114218146554">Partition. Rows with the same value in partition columns are considered to be in the same window.</p>
</td>
</tr>
<tr id="dli_spark_rank__en-us_topic_0000001655540562_row316019148205"><td class="cellrowborder" valign="top" width="22.759999999999998%" headers="mcps1.3.4.2.2.4.1.1 "><p id="dli_spark_rank__en-us_topic_0000001655540562_p12181324182913">orderby_clause</p>
</td>
<td class="cellrowborder" valign="top" width="15.920000000000002%" headers="mcps1.3.4.2.2.4.1.2 "><p id="dli_spark_rank__en-us_topic_0000001655540562_p91821324192918">No</p>
</td>
<td class="cellrowborder" valign="top" width="61.31999999999999%" headers="mcps1.3.4.2.2.4.1.3 "><p id="dli_spark_rank__en-us_topic_0000001655540562_p418272442914">It is used to specify how data is sorted in a window.</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section" id="dli_spark_rank__en-us_topic_0000001655540562_section210162513312"><h4 class="sectiontitle">Return Values</h4><p id="dli_spark_rank__en-us_topic_0000001655540562_p184124317231">The return value is of the INT type.</p>
<div class="note" id="dli_spark_rank__en-us_topic_0000001655540562_note61422099395"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="dli_spark_rank__en-us_topic_0000001655540562_p11468110152814">If the value of <strong id="dli_spark_rank__en-us_topic_0000001655540562_b23526527331323">a</strong> is <strong id="dli_spark_rank__en-us_topic_0000001655540562_b14670814831323">NULL</strong>, <strong id="dli_spark_rank__en-us_topic_0000001655540562_b154229504831323">NULL</strong> is returned.</p>
</div></div>
</div>
<div class="section" id="dli_spark_rank__en-us_topic_0000001655540562_section13277192233920"><h4 class="sectiontitle">Example Code</h4><div class="p" id="dli_spark_rank__en-us_topic_0000001655540562_p10444251705">To help you understand how to use functions, this example provides source data and function examples based on the source data. Run the following command to create the <strong id="dli_spark_rank__en-us_topic_0000001655540562_b12585256073165">logs</strong> table and add data:<pre class="screen" id="dli_spark_rank__en-us_topic_0000001655540562_screen17444185119011">CREATE TABLE logs (
cookieid string,
createtime string,
pv INT
) ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
stored as textfile;</pre>
</div>
<p id="dli_spark_rank__en-us_topic_0000001655540562_p134447516010">Adds the following data:</p>
<pre class="screen" id="dli_spark_rank__en-us_topic_0000001655540562_screen204448513013">cookie1 2015-04-10 1
cookie1 2015-04-11 5
cookie1 2015-04-12 7
cookie1 2015-04-13 3
cookie1 2015-04-14 2
cookie1 2015-04-15 4
cookie1 2015-04-16 4
cookie2 2015-04-10 2
cookie2 2015-04-11 3
cookie2 2015-04-12 5
cookie2 2015-04-13 6
cookie2 2015-04-14 3
cookie2 2015-04-15 9
cookie2 2015-04-16 7</pre>
<p id="dli_spark_rank__en-us_topic_0000001655540562_p1444513511008">Example: Groups all records by <strong id="dli_spark_rank__en-us_topic_0000001655540562_b032116278502">cookieid</strong>, sorts them by <strong id="dli_spark_rank__en-us_topic_0000001655540562_b9753123385012">pv</strong> in descending order, and returns the sequence number of each row in the group. An example command is as follows:</p>
<pre class="screen" id="dli_spark_rank__en-us_topic_0000001655540562_screen344514511906">select cookieid, createtime, pv,
rank() over(partition by cookieid order by pv desc) as rank
from logs
where cookieid = 'cookie1';
-- Result:
cookie1 2015-04-12 7 1
cookie1 2015-04-11 5 2
cookie1 2015-04-16 4 3 (third in parallel)
cookie1 2015-04-15 4 3
cookie1 2015-04-13 3 5 (skip 4 and start from 5)
cookie1 2015-04-14 2 6
cookie1 2015-04-10 1 7</pre>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="dli_08_0475.html">Window Functions</a></div>
</div>
</div>