SQL Server分区顺序 - 即使行相同,也不会绑定DenseRank值

时间:2017-07-12 14:39:06

标签: sql sql-server partition

enter image description here

这个问题最好用图像和我目前的脚本解释...如何为每个分配提取一行,最低等级,如果有2行密集为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

不再需要一个随机的第一个Tied'第一名',现在需要得到一个具有最高日差异,然后也是最高金额之后。 SO已适应此版本3中的所有内容。

2 个答案:

答案 0 :(得分:2)

您似乎不想使用DENSE_RANKROW_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

答案 1 :(得分:1)

由&new;(' newid()'作为“决胜局”'

Order by [Text],Newid()