Excel排名与SQL排名

时间:2018-12-07 10:06:21

标签: sql-server excel

我面临以下困境。

我需要在Excel中rank数据,就像在SQL中对数据进行排名一样,如下所示:

declare @table table
(id int identity primary key , batchid int)

insert into @table (batchid) select 10
insert into @table (batchid) select 35
insert into @table (batchid) select 35
insert into @table (batchid) select 35
insert into @table (batchid) select 8
insert into @table (batchid) select 21
insert into @table (batchid) select 10
insert into @table (batchid) select 11
insert into @table (batchid) select 4
insert into @table (batchid) select 10
insert into @table (batchid) select 11
insert into @table (batchid) select 4
insert into @table (batchid) select 21
insert into @table (batchid) select 8
insert into @table (batchid) select 21
insert into @table (batchid) select 8
insert into @table (batchid) select 21
insert into @table (batchid) select 4
insert into @table (batchid) select 21
insert into @table (batchid) select 10
insert into @table (batchid) select 21
insert into @table (batchid) select 11
insert into @table (batchid) select 21
insert into @table (batchid) select 8
insert into @table (batchid) select 21
insert into @table (batchid) select 10
insert into @table (batchid) select 21
insert into @table (batchid) select 10
insert into @table (batchid) select 21

select
batchid
,   rank() over (partition by batchid order by id)  [RANK]
from    @table

我看了很多 YouTube 视频和如何在 Excel rank的教程,但是我看不到{{1} } partition by,如上面的代码所示。

请参见上面脚本的以下结果:

enter image description here

我可以在Excel中使用什么公式来获得相同的结果?

2 个答案:

答案 0 :(得分:0)

使用简单的公式非常容易:

enter image description here

在单元格B1中输入初始值1。然后,在B2中输入公式:

=IF(A1=A2,B1+1,1)

然后将其一直向下拖动。

答案 1 :(得分:0)

选中SUMPRODUCThere中的示例:

  

选择数据旁边的空白单元格(例如C2),键入   公式

=SUMPRODUCT(($A$2:$A$11=A2)*(B2<$B$2:$B$11))+1 
  

然后向下拖动自动填充手柄,以将此公式应用于所需的单元格。

     

在公式中,A2:A11是包含组值的范围,A2是   组值列表中的第一个单元格,B2是中的第一个单元格   需要排名的列表,B2:B11是包含值的列表   需要排名。

enter image description here