LoginSignup
0
0

bigqueryのwindow関数でNULLを無視する(dense_rank編)

Posted at

SQL

with
target as (
  select 1 as val union all
  select 1 as val union all
  select null as val union all
  select 2 as val union all
  select 3 as val union all
  select null as val union all
  select 4 as val union all
  select 4 as val
)
select
  val,
  case
    when val is null then null
    else dense_rank() over (order by val nulls last)
  end as dense_rank_val,
from
  target

結果

image.png

ちょっと補足

構文的にはNULLを無視してランクを採番する方法がないので少しハマったのでメモ。
CASEによる人力判定とnulls lastを使えば何とかできる。
もっとスマートな方法がありましたら教えて下さい。

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