7.16.3. window_rank#

7.16.3.1. 概要#

バージョン 11.0.9 で追加.

このウィンドウ関数はギャップを含んだ各レコードの順位を計算します。 window_record_number に似ています。 window_record_number は各レコードが何番目のレコードかを計算します。何番目かは常に増加しますが、順位は複数のレコードが同じ順位の場合は増加しません。同じ順位のレコードが複数あった後の順位にはギャップがあります。ソートキーの値が 100, 100, 200 だった場合、順位はそれぞれ 1, 1, 3 になります。最後のレコードの順位は 2 ではなく 3 です。なぜなら順位が 1 のレコードが2つあるからです。

7.16.3.2. 構文#

このウィンドウ関数には引数はありません。

window_rank()

7.16.3.3. 使い方#

使い方を示すために使うスキーマ定義とサンプルデータは以下の通りです。

実行例:

table_create Points TABLE_NO_KEY
# [[0,1337566253.89858,0.000355720520019531],true]
column_create Points game COLUMN_SCALAR ShortText
# [[0,1337566253.89858,0.000355720520019531],true]
column_create Points score COLUMN_SCALAR UInt32
# [[0,1337566253.89858,0.000355720520019531],true]
load --table Points
[
["game",  "score"],
["game1", 100],
["game1", 200],
["game1", 100],
["game1", 400],
["game2", 150],
["game2", 200],
["game2", 200],
["game2", 200]
]
# [[0,1337566253.89858,0.000355720520019531],8]

以下はソートキーのみを指定した例です。

実行例:

select Points \
  --columns[rank].stage filtered \
  --columns[rank].value 'window_rank()' \
  --columns[rank].type UInt32 \
  --columns[rank].window.sort_keys score \
  --output_columns 'game, score, rank' \
  --sort_keys score
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         8
#       ],
#       [
#         [
#           "game",
#           "ShortText"
#         ],
#         [
#           "score",
#           "UInt32"
#         ],
#         [
#           "rank",
#           "UInt32"
#         ]
#       ],
#       [
#         "game1",
#         100,
#         1
#       ],
#       [
#         "game1",
#         100,
#         1
#       ],
#       [
#         "game2",
#         150,
#         3
#       ],
#       [
#         "game2",
#         200,
#         4
#       ],
#       [
#         "game2",
#         200,
#         4
#       ],
#       [
#         "game1",
#         200,
#         4
#       ],
#       [
#         "game2",
#         200,
#         4
#       ],
#       [
#         "game1",
#         400,
#         8
#       ]
#     ]
#   ]
# ]

以下は各ゲームごとに順位を計算する例です。

実行例:

select Points \
  --columns[rank].stage filtered \
  --columns[rank].value 'window_rank()' \
  --columns[rank].type UInt32 \
  --columns[rank].window.group_keys game \
  --columns[rank].window.sort_keys score \
  --output_columns 'game, score, rank' \
  --sort_keys game,score
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         8
#       ],
#       [
#         [
#           "game",
#           "ShortText"
#         ],
#         [
#           "score",
#           "UInt32"
#         ],
#         [
#           "rank",
#           "UInt32"
#         ]
#       ],
#       [
#         "game1",
#         100,
#         1
#       ],
#       [
#         "game1",
#         100,
#         1
#       ],
#       [
#         "game1",
#         200,
#         3
#       ],
#       [
#         "game1",
#         400,
#         4
#       ],
#       [
#         "game2",
#         150,
#         1
#       ],
#       [
#         "game2",
#         200,
#         2
#       ],
#       [
#         "game2",
#         200,
#         2
#       ],
#       [
#         "game2",
#         200,
#         2
#       ]
#     ]
#   ]
# ]

以下は降順を使う例です。

実行例:

select Points \
  --columns[rank].stage filtered \
  --columns[rank].value 'window_rank()' \
  --columns[rank].type UInt32 \
  --columns[rank].window.group_keys game \
  --columns[rank].window.sort_keys -score \
  --output_columns 'game, score, rank' \
  --sort_keys game,-score
# [
#   [
#     0,
#     1337566253.89858,
#     0.000355720520019531
#   ],
#   [
#     [
#       [
#         8
#       ],
#       [
#         [
#           "game",
#           "ShortText"
#         ],
#         [
#           "score",
#           "UInt32"
#         ],
#         [
#           "rank",
#           "UInt32"
#         ]
#       ],
#       [
#         "game1",
#         400,
#         1
#       ],
#       [
#         "game1",
#         200,
#         2
#       ],
#       [
#         "game1",
#         100,
#         3
#       ],
#       [
#         "game1",
#         100,
#         3
#       ],
#       [
#         "game2",
#         200,
#         1
#       ],
#       [
#         "game2",
#         200,
#         1
#       ],
#       [
#         "game2",
#         200,
#         1
#       ],
#       [
#         "game2",
#         150,
#         4
#       ]
#     ]
#   ]
# ]

7.16.3.4. 引数#

なし。

7.16.3.5. 戻り値#

順位です。 UInt32 の値になります。

7.16.3.6. 参考#