Debian Codegraph @

doc/source/reference/window_functions/window_rank.rst

raw
1 .. -*- rst -*-
2
3 .. groonga-command
4 .. database: window_functions_window_rank
5
6 ``window_rank``
7 ===============
8
9 Summary
10 -------
11
12 .. versionadded:: 11.0.9
13
14 This window function computes the rank of each record with gaps. This
15 is similar to :doc:`window_record_number`. :doc:`window_record_number`
16 computes the number of each record. The number is always incremented
17 but the rank isn't incremented when multiple records that are the same
18 order. The next rank after multiple records that are the same order
19 has gap. If values of sort keys are ``100, 100, 200`` then the ranks
20 of them are ``1, 1, 3``. The rank of the last record is ``3`` not
21 ``2`` because there are two ``1`` rank records.
22
23 Syntax
24 ------
25
26 This window function doesn't require any parameters::
27
28 window_rank()
29
30 Usage
31 -----
32
33 Here are a schema definition and sample data to show usage.
34
35 .. groonga-command
36 .. include:: ../../example/reference/window_functions/window_rank/usage_setup.log
37 .. table_create Points TABLE_NO_KEY
38 .. column_create Points game COLUMN_SCALAR ShortText
39 .. column_create Points score COLUMN_SCALAR UInt32
40 ..
41 .. load --table Points
42 .. [
43 .. ["game", "score"],
44 .. ["game1", 100],
45 .. ["game1", 200],
46 .. ["game1", 100],
47 .. ["game1", 400],
48 .. ["game2", 150],
49 .. ["game2", 200],
50 .. ["game2", 200],
51 .. ["game2", 200]
52 .. ]
53
54 Here is an example that specifies only sort keys:
55
56 .. groonga-command
57 .. include:: ../../example/reference/window_functions/window_rank/usage_sort_keys.log
58 .. select Points \
59 .. --columns[rank].stage filtered \
60 .. --columns[rank].value 'window_rank()' \
61 .. --columns[rank].type UInt32 \
62 .. --columns[rank].window.sort_keys score \
63 .. --output_columns 'game, score, rank' \
64 .. --sort_keys score
65
66 Here is an example that computes ranks for each game:
67
68 .. groonga-command
69 .. include:: ../../example/reference/window_functions/window_rank/usage_group.log
70 .. select Points \
71 .. --columns[rank].stage filtered \
72 .. --columns[rank].value 'window_rank()' \
73 .. --columns[rank].type UInt32 \
74 .. --columns[rank].window.group_keys game \
75 .. --columns[rank].window.sort_keys score \
76 .. --output_columns 'game, score, rank' \
77 .. --sort_keys game,score
78
79 Here is an example that uses descending order:
80
81 .. groonga-command
82 .. include:: ../../example/reference/window_functions/window_rank/usage_group_descending.log
83 .. select Points \
84 .. --columns[rank].stage filtered \
85 .. --columns[rank].value 'window_rank()' \
86 .. --columns[rank].type UInt32 \
87 .. --columns[rank].window.group_keys game \
88 .. --columns[rank].window.sort_keys -score \
89 .. --output_columns 'game, score, rank' \
90 .. --sort_keys game,-score
91
92 Parameters
93 ----------
94
95 Nothing.
96
97 Return value
98 ------------
99
100 The rank as ``UInt32`` value.
101
102 See also
103 --------
104
105 * :doc:`window_record_number`
106