Dense_rank排序超过999个字符

时间:2018-10-11 14:25:27

标签: sql oracle oracle11g window-functions

我们刚刚在Oracle数据库上发现了一个奇怪的行为

select dense_rank() over (order by val) rank, val
from (
  select lpad('a', 998, 'a') || '0' val from dual
  union all
  select lpad('a', 998, 'a') || '1' val from dual
);

将按预期返回:

RANK |VAL       
-----|-----------------
1    |aaaa[...]aaaaa0
2    |aaaa[...]aaaaa1

但是我们再添加1个字符

select dense_rank() over (order by val) rank, val
from (
  select lpad('a', 999, 'a') || '0' val from dual
  union all
  select lpad('a', 999, 'a') || '1' val from dual
);

结果出乎意料:

RANK |VAL       
-----|-----------------
1    |aaaa[...]aaaaa0
1    |aaaa[...]aaaaa1

所以我必须写:

select dense_rank() over (order by val, substr(val, 999, 999)) rank, val
from (
  select lpad('a', 999, 'a') || '0' val from dual
  union all
  select lpad('a', 999, 'a') || '1' val from dual
);

有人可以解释为什么会发生这种情况吗? 如何防止这种情况发生? 感谢

0 个答案:

没有答案