--■■■逆ソートを使う方法■■■
select ID,Val,
-1+dense_rank() over(partition by ID order by Val asc)
+dense_rank() over(partition by ID order by Val desc)
as distinctVal
from (select 1 as ID,10 as Val from sysibm.sysdummy1
union all select 1 as ID,10 as Val from sysibm.sysdummy1
union all select 1 as ID,20 as Val from sysibm.sysdummy1
union all select 1 as ID,20 as Val from sysibm.sysdummy1
union all select 2 as ID,30 as Val from sysibm.sysdummy1
union all select 2 as ID,30 as Val from sysibm.sysdummy1
union all select 2 as ID,30 as Val from sysibm.sysdummy1
union all select 2 as ID,30 as Val from sysibm.sysdummy1
union all select 3 as ID,40 as Val from sysibm.sysdummy1
union all select 3 as ID,40 as Val from sysibm.sysdummy1
union all select 3 as ID,50 as Val from sysibm.sysdummy1
union all select 4 as ID,60 as Val from sysibm.sysdummy1
union all select 4 as ID,70 as Val from sysibm.sysdummy1
union all select 4 as ID,80 as Val from sysibm.sysdummy1) dummy
order by ID,Val;
--■■■dense_rankの最大値を求める方法■■■
select ID,Val,max(Rank) over(partition by ID) as distinctVal
from (select ID,Val,dense_rank() over(partition by ID order by Val) as Rank
from (select 1 as ID,10 as Val from sysibm.sysdummy1
union all select 1 as ID,10 as Val from sysibm.sysdummy1
union all select 1 as ID,20 as Val from sysibm.sysdummy1
union all select 1 as ID,20 as Val from sysibm.sysdummy1
union all select 2 as ID,30 as Val from sysibm.sysdummy1
union all select 2 as ID,30 as Val from sysibm.sysdummy1
union all select 2 as ID,30 as Val from sysibm.sysdummy1
union all select 2 as ID,30 as Val from sysibm.sysdummy1
union all select 3 as ID,40 as Val from sysibm.sysdummy1
union all select 3 as ID,40 as Val from sysibm.sysdummy1
union all select 3 as ID,50 as Val from sysibm.sysdummy1
union all select 4 as ID,60 as Val from sysibm.sysdummy1
union all select 4 as ID,70 as Val from sysibm.sysdummy1
union all select 4 as ID,80 as Val from sysibm.sysdummy1) dummy ) dummy
order by ID,Val;