雪花 - Dense_rank 从 2 而不是 1 开始
Snowflake - Dense_rank starting at 2 rather than 1
我在查询中使用了以下语句:
CASE WHEN apercent IS NULL THEN NULL ELSE dense_rank() over (partition by adate order by apercent desc) END as arank
出于某种原因,排名结果从 2 而不是 1 开始。没有绑定值,但有一些 NULL 值。我假设这是因为 NULL。有什么方法可以更新它以强制排名从 1 开始?
当前结果:
arank
apercent
2
23.57
3
13.61
4
10.67
5
6.14
6
6.00
7
5.79
8
5.57
9
5.27
10
4.80
NULL
NULL
期望的结果:
arank
apercent
1
23.57
2
13.61
3
10.67
4
6.14
5
6.00
6
5.79
7
5.57
8
5.27
9
4.80
NULL
NULL
一个简单的解决方案是使用 NULLS LAST
:
(CASE WHEN apercent IS NOT NULL
THEN dense_rank() over (partition by adate order by apercent desc nulls last)
END) as arank
或者,您可以按 NULL
值进行分区,这样它们就不会包含在非 NULL
值的计数中:
(CASE WHEN apercent IS NOT NULL
THEN dense_rank() over (partition by adate, apercent is null order by apercent desc)
END) as arank
如果您的百分比不能为负数,您可以使用 coalesce
模仿 nulls last
case when apercent is null then null
else dense_rank() over (partition by adate order by coalesce(apercent,0.00) desc)
end as arank
我在查询中使用了以下语句:
CASE WHEN apercent IS NULL THEN NULL ELSE dense_rank() over (partition by adate order by apercent desc) END as arank
出于某种原因,排名结果从 2 而不是 1 开始。没有绑定值,但有一些 NULL 值。我假设这是因为 NULL。有什么方法可以更新它以强制排名从 1 开始?
当前结果:
arank | apercent |
---|---|
2 | 23.57 |
3 | 13.61 |
4 | 10.67 |
5 | 6.14 |
6 | 6.00 |
7 | 5.79 |
8 | 5.57 |
9 | 5.27 |
10 | 4.80 |
NULL | NULL |
期望的结果:
arank | apercent |
---|---|
1 | 23.57 |
2 | 13.61 |
3 | 10.67 |
4 | 6.14 |
5 | 6.00 |
6 | 5.79 |
7 | 5.57 |
8 | 5.27 |
9 | 4.80 |
NULL | NULL |
一个简单的解决方案是使用 NULLS LAST
:
(CASE WHEN apercent IS NOT NULL
THEN dense_rank() over (partition by adate order by apercent desc nulls last)
END) as arank
或者,您可以按 NULL
值进行分区,这样它们就不会包含在非 NULL
值的计数中:
(CASE WHEN apercent IS NOT NULL
THEN dense_rank() over (partition by adate, apercent is null order by apercent desc)
END) as arank
如果您的百分比不能为负数,您可以使用 coalesce
nulls last
case when apercent is null then null
else dense_rank() over (partition by adate order by coalesce(apercent,0.00) desc)
end as arank