0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

BigQueryの番号付け関数が返す値の比較

Last updated at Posted at 2021-02-17

BigQueryの番号付け関数が返す値の違いが、いつも「あれ、RANKとROW_NUMBERってどう違うんだっけ」ってドキュメント見ることになるので、実際にクエリ書いて整理してみた。

SELECT 
  value,
  ROW_NUMBER() OVER (ORDER BY value) row_number,
  RANK() OVER (ORDER BY value) rank,
  DENSE_RANK() OVER (ORDER BY value) dense_rank,
  PERCENT_RANK() OVER (ORDER BY value) percent_rank,
  CUME_DIST() OVER (ORDER BY value) cume_dist,
  NTILE(2) OVER (ORDER BY value) ntile2,
  NTILE(5) OVER (ORDER BY value) ntile5,
  NTILE(10) OVER (ORDER BY value) ntile10,
FROM
(
  SELECT 0 AS value
  UNION ALL
  SELECT 1 AS value
  UNION ALL
  SELECT 1 AS value
  UNION ALL
  SELECT 2 AS value
  UNION ALL
  SELECT 3 AS value
  UNION ALL
  SELECT 20 AS value
)
value row_number rank dense_rank percent_rank cume_dist ntile2 ntile5 ntile10
0 1 1 1 0.0 0.16666666666666666 1 1 1
1 2 2 2 0.2 0.5 1 1 2
1 3 2 2 0.2 0.5 1 2 3
2 4 4 3 0.6 0.6666666666666666 2 3 4
3 5 5 4 0.8 0.8333333333333334 2 4 5
20 6 6 5 1.0 1.0 2 5 6

NTILEは、指定した数字のバケットに、値を均等に分割する。値自体は見ず、その順序のみでグルーピングして、可能な限り同じ数ずつに分けようとする。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?