简单的排名公式
我正在寻找一个数学排名公式。
示例是
2008 2009 2010
A 5 6 4
B 6 7 5
C 7 8 2
我想为每个周期代码字段添加一个排名列,
rank
2008 2009 2010 2008 2009 2010
B 6 7 5 2 1 1
A 5 6 4 3 2 2
C 7 2 2 1 3 3
请不要使用循环遍历行和列的方法进行回复,随着排名值的增加而增加排名值,这很简单。我正在寻找一个公式,就像查找总数百分比(项目/总数)一样。我知道我以前见过这个,但很难找到它。
提前致谢!
I'm looking for a mathmatical ranking formula.
Sample is
2008 2009 2010
A 5 6 4
B 6 7 5
C 7 8 2
I want to add a rank column for each period code field
rank
2008 2009 2010 2008 2009 2010
B 6 7 5 2 1 1
A 5 6 4 3 2 2
C 7 2 2 1 3 3
please do not reply with methods that loop thru the rows and columns, incrementing the rank value as it goes, that's easy. I'm looking for a formula much like finding the percent total (item / total). I know i've seen this before but an havning a tough time locating it.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
sort ((letters_col, number_col)scending by number_col)
与排序算法一样高效。
然后对行进行编号,当然
编辑
我真的对你的评论感到不安“请不要对这个答案进行投票,排序和循环不是我所要求的。我在原来的问题中特别指出了这一点。” ,以及反对票,因为正如您可能已经从收到的各种答案中注意到的那样,它基本上是正确的。
然而,我仍然在思考你可能在哪里以及如何“以前见过这个”。
好吧,我想我得到了答案:你在 Excel 中看到了这个。
看看这个:
这是输入公式并按H列排序后的结果。
这正是你想要的......
sort ((letters_col, number_col) descending by number_col)
As efficient as your sort alg.
Then number the rows, of course
Edit
I really got upset by your comment "please don't up vote this answer, sorting and loop is not what I'm asking for. i specifically stated this in my original question. " , and the negative votes, because, as you may have noted by the various answers received, it's basically correct.
However, I remained pondering where and how you may "have seen this before".
Well, I think I got the answer: You saw this in Excel.
Look at this:
This is the result after entering the formulas and sorting by column H.
It's exactly what you want ...
你用什么?如果您使用 Excel,您需要查找 RANK(num, ref)。
我不知道有任何编程语言内置了它,它总是需要某种形式的循环。
What are you using? If you're using Excel, you're looking for RANK(num, ref).
I don't know of any programming language that has that built in, it would always require a loop of some form.
单个元素的排名,您可以通过循环遍历元素,计算有多少个元素的值高于给定元素,然后加 1,在 O(n) 内完成。
如果您想要 对所有元素进行排名,最好的(也是唯一的)方法是对元素进行排序。你做的任何其他事情都相当于排序(没有“公式”)
If you want the rank of a single element, you can do it in O(n) by looping through the elements, counting how many have value above the given element, and adding 1.
If you want the rank of all the elements, the best (and really only) way is to sort the elements. Anything else you do will be equivalent to sorting (there is no "formula")
您使用的是 T-SQL 吗? T-SQL RANK() 可能会提取您想要的内容。
Are you using T-SQL? T-SQL RANK() may pull what you want.