SQL 服务器分区顺序 - 即使行相同也不会绑定 DenseRank 值
SQL Server Partition Order - No tie DenseRank values even if rows are same
这个问题最好用一张图片和我目前拥有的脚本来解释...我如何才能在每次分配时提取完整的一行,排名最低,并且如果有 2 行的 denserank 为 1,然后选择其中一个?...
select *
,Dense_RANK() over (partition by [Assignment] order by [Text] desc) as
[DenseRank]
from [dbo].[CLEANSED_T3B_Step1_Res_Withdups____CP]
select * from
(
select *
,Dense_RANK() over (partition by [Assignment] order by [Text] desc, NewID()
) as [DenseRank] from [dbo].[CLEANSED_T3B_Step1_Res_Withdups____CP]
) as A
where A.[DenseRank] = 1
第二个脚本运行完美!
SELECT * INTO
[dbo].[CLEANSED_T3B_Step1_COMPLETED]
from
(
select *
,Dense_RANK() over (partition by [Assignment] order by
left([Text],1) desc , [Diff_Doc_Clearing_Date] desc , [Amount] asc
as [DenseRank]
from [dbo].[CLEANSED_T3B_Step1_Res_Withdups____CP]
)
as A
where A.[DenseRank] = 1
不再只需要一个随机的第一名并列“第一名”,现在需要获得最高日差的那个,然后也是最高的。所以在这个版本 3 中调整了所有内容。
您似乎不想使用 DENSE_RANK
但 ROW_NUMBER
.
with cte as(
select t.*, rn = row_number() over(partition by assignment order by [text] desc)
from tablename t
)
select * from cte
where rn = 1
按 'newid()' 作为 'tie-breaker'
排序
Order by [Text],Newid()
这个问题最好用一张图片和我目前拥有的脚本来解释...我如何才能在每次分配时提取完整的一行,排名最低,并且如果有 2 行的 denserank 为 1,然后选择其中一个?...
select *
,Dense_RANK() over (partition by [Assignment] order by [Text] desc) as
[DenseRank]
from [dbo].[CLEANSED_T3B_Step1_Res_Withdups____CP]
select * from
(
select *
,Dense_RANK() over (partition by [Assignment] order by [Text] desc, NewID()
) as [DenseRank] from [dbo].[CLEANSED_T3B_Step1_Res_Withdups____CP]
) as A
where A.[DenseRank] = 1
第二个脚本运行完美!
SELECT * INTO
[dbo].[CLEANSED_T3B_Step1_COMPLETED]
from
(
select *
,Dense_RANK() over (partition by [Assignment] order by
left([Text],1) desc , [Diff_Doc_Clearing_Date] desc , [Amount] asc
as [DenseRank]
from [dbo].[CLEANSED_T3B_Step1_Res_Withdups____CP]
)
as A
where A.[DenseRank] = 1
不再只需要一个随机的第一名并列“第一名”,现在需要获得最高日差的那个,然后也是最高的。所以在这个版本 3 中调整了所有内容。
您似乎不想使用 DENSE_RANK
但 ROW_NUMBER
.
with cte as(
select t.*, rn = row_number() over(partition by assignment order by [text] desc)
from tablename t
)
select * from cte
where rn = 1
按 'newid()' 作为 'tie-breaker'
排序Order by [Text],Newid()