Пока это не отвечает на ваш вопрос. Это не разделение, это проблема, это, кажется, какая-то странная проблема, когда CAST и Dense_Rank запускаются дважды в одном и том же SELECT.
Рассмотрим:
select
num,
case
when num is null then 0
else cast(dense_rank() over (partition by case when num is not null then 1 else 2 end order by num) as number)
end as str_rnk,
case
when num is null then 0
else cast(dense_rank() over (partition by case when num is not null then 1 else 2 end order by num) as number)
end as str_rnk2
from tmp
cross join (
select cast(count(num) as number) as nn from tmp
) q;
+--------+---------+----------+
| num | str_rnk | str_rnk2 |
+--------+---------+----------+
| 1 | 1 | 1 |
| 1 | 1 | 1 |
| 2 | 2 | 3 |
| 2 | 2 | 3 |
| 4 | 3 | 5 |
| 4 | 3 | 5 |
| 9 | 4 | 7 |
| 10 | 5 | 8 |
| 10 | 5 | 8 |
| 11 | 6 | 10 |
| <null> | 0 | 0 |
| <null> | 0 | 0 |
| <null> | 0 | 0 |
+--------+---------+----------+
Так как CAST здесь не нужен:
select
num,
case
when num is null then 0
else dense_rank() over (partition by case when num is not null then 1 else 2 END order by num)
end as str_rnk,
case
when num is null then 0
else dense_rank() over (partition by case when num is not null then 1 else 2 END order by num)
end as str_rnk2
from tmp
cross join (
select cast(count(num) as number) as nn from tmp
) q;
+--------+---------+----------+
| num | str_rnk | str_rnk2 |
+--------+---------+----------+
| 1 | 1 | 1 |
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 2 | 2 | 2 |
| 4 | 3 | 3 |
| 4 | 3 | 3 |
| 9 | 4 | 4 |
| 10 | 5 | 5 |
| 10 | 5 | 5 |
| 11 | 6 | 6 |
| <null> | 0 | 0 |
| <null> | 0 | 0 |
| <null> | 0 | 0 |
+--------+---------+----------+
Ваш запрос с быстрой переписью:
select
num,
case
when num is null then 0
else dense_rank() over (partition by num * 0 order by num)
end as str_rnk,
str_rnk * 1.0/COUNT(*) OVER (PARTITION BY num * 0) as pct_rnk
from tmp
order by num
;
+--------+---------+---------+
| num | str_rnk | pct_rnk |
+--------+---------+---------+
| <null> | 0 | 0.0 |
| <null> | 0 | 0.0 |
| <null> | 0 | 0.0 |
| 1 | 1 | 0.1 |
| 1 | 1 | 0.1 |
| 2 | 2 | 0.2 |
| 2 | 2 | 0.2 |
| 4 | 3 | 0.3 |
| 4 | 3 | 0.3 |
| 9 | 4 | 0.4 |
| 10 | 5 | 0.5 |
| 10 | 5 | 0.5 |
| 11 | 6 | 0.6 |
+--------+---------+---------+
Или, если вы хотите полностью получить оператор CASE:
select
num,
dense_rank() over (partition by num * 0 order by num) * (num * 0 + 1.0) as str_rnk,
str_rnk/COUNT(*) OVER (PARTITION BY num * 0) as pct_rnk
from tmp
order by num;