【发布时间】:2018-05-07 16:38:58
【问题描述】:
我有一位同事不想在百分等级中包含空行。默认的 Teradata 函数似乎只是将 null 视为集合中的最小数字,因此我决定手动进行数学运算。我开始使用下面的查询来测试我的方程式
drop table tmp;
create multiset volatile table tmp (
num byteint
) primary index (num)
on commit preserve rows
;
insert into tmp
values (1)
;insert into tmp
values (2)
;insert into tmp
values (1)
;insert into tmp
values (4)
;insert into tmp
values (null)
;insert into tmp
values (4)
;insert into tmp
values (null)
;insert into tmp
values (2)
;insert into tmp
values (9)
;insert into tmp
values (null)
;insert into tmp
values (10)
;insert into tmp
values (10)
;insert into tmp
values (11)
;
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,
q.nn,
str_rnk/q.nn as pct_rnk
from tmp
cross join (
select cast(count(num) as number) as nn from tmp
) q
order by num
;
所以我希望在结果集中看到的是:
num str_rnk nn pct_rnk
null 0 10 0
null 0 10 0
null 0 10 0
1 1 10 0.1
1 1 10 0.1
2 2 10 0.2
2 2 10 0.2
4 3 10 0.3
4 3 10 0.3
9 4 10 0.4
10 5 10 0.5
10 5 10 0.5
但我得到的结果看起来像是常规的rank 而不是dense_rank,如下所示:
num str_rnk nn pct_rnk
null 0 10 0
null 0 10 0
null 0 10 0
1 1 10 0.1
1 1 10 0.1
2 2 10 0.3
2 2 10 0.3
4 3 10 0.5
4 3 10 0.5
9 4 10 0.7
10 5 10 0.8
10 5 10 0.8
我知道我可以在子查询中设置排名,它会按照我期望的方式进行计算,但为什么不按照我现在的方式进行呢?
【问题讨论】:
标签: teradata rank percentile dense-rank