使用 DENSE_RANK 排除空值
Exclude null values using DENSE_RANK
Dense_Rank 什么都考虑了。有没有办法排除空值,以便 1 之后的下一个等级为 2 而不是 3。
这是 table 现在的样子:
A | DENSE_R
--------------
1 | 1
--------------
2 | null
--------------
3 | 3
--------------
4 | 4
这就是我想要 table 的样子:
A | DENSE_R
--------------
1 | 1
--------------
2 | null
--------------
3 | 2
--------------
4 | 3
我正在使用以下代码来执行此操作:-
WITH CTE AS
(
SELECT A
FROM A1
)
SELECT A,
CASE
WHEN **Condition**
THEN DENSE_RANK() OVER (Order by [A] ASC)
END
AS 'DENSE_R'
FROM CTE
您可以这样使用 case
:
select A,
(case when A is not null
then dense_rank() over (partition by (case when A is not null then 1 else 0 end)
order by a desc
)
end) as dr
from A1;
使用 partition by
与您已经使用的相同 **Condition**
。
WITH CTE AS
(
SELECT A
FROM A1
)
SELECT A,
CASE
WHEN **Condition**
THEN DENSE_RANK() OVER (Partition by (case when **Condition** then 1 else 0 end) Order by [A] ASC)
END
AS 'DENSE_R'
FROM CTE
Dense_Rank 什么都考虑了。有没有办法排除空值,以便 1 之后的下一个等级为 2 而不是 3。
这是 table 现在的样子:
A | DENSE_R
--------------
1 | 1
--------------
2 | null
--------------
3 | 3
--------------
4 | 4
这就是我想要 table 的样子:
A | DENSE_R
--------------
1 | 1
--------------
2 | null
--------------
3 | 2
--------------
4 | 3
我正在使用以下代码来执行此操作:-
WITH CTE AS
(
SELECT A
FROM A1
)
SELECT A,
CASE
WHEN **Condition**
THEN DENSE_RANK() OVER (Order by [A] ASC)
END
AS 'DENSE_R'
FROM CTE
您可以这样使用 case
:
select A,
(case when A is not null
then dense_rank() over (partition by (case when A is not null then 1 else 0 end)
order by a desc
)
end) as dr
from A1;
使用 partition by
与您已经使用的相同 **Condition**
。
WITH CTE AS
(
SELECT A
FROM A1
)
SELECT A,
CASE
WHEN **Condition**
THEN DENSE_RANK() OVER (Partition by (case when **Condition** then 1 else 0 end) Order by [A] ASC)
END
AS 'DENSE_R'
FROM CTE