本文介绍了如何将最高金额排名为最低金额的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有这张桌子 ID NAME AMOUNT 1 AAA 50000 2 BBB 49999 3 CCC 15001 4 DDD 28000 5 KKK 15000 6 EEE 28005 7 SSS 50000 i需要排名如此50000 1st,49999 2nd,3rd 35000 ..同样增加的金额将获得最高排名。 我尝试了什么: i尝试过rank,dense_rank,ntile,row count i have this table ID NAME AMOUNT 1 AAA 500002 BBB 499993 CCC 150014 DDD 280005 KKK 150006 EEE 280057 SSS 50000i need ranking like this 50000 1st, 49999 2nd , 3rd 35000.. likewise increased amount will get highest rank .What I have tried:i have tried rank,dense_rank,ntile,row count推荐答案 SELECT * FROM MyTable ORDER BY Amount DESC 如果您要存储金额在VARCHAR或NVARCHAR列中 - 在这种情况下,比较将基于字符串而不是数字,整个比较将基于第一个不同的角色。所以你的排序顺序将变为 The only reason that might not give you what you want is if you are storing Amount in a VARCHAR or NVARCHAR column - in which case the comparison will be string based instead of numeric, and the whole comparison will be based on teh first different character. So your sort order would become1101112...181922021...如果是,请将数据库更改为使用数字列。 b $ b 你的意思是: If you are, then change your DB to use a numeric column instead.[edit]Do you mean:SELECT ID, Name, Amount, RANK () OVER (ORDER BY Amount DESC) as Rank FROM MyTableORDER BY Amount DESC 哪个会给你: Which will give you:IDName AmountRank1AAA 500014SSS 500012BBB 499933CCC 15004 或: Or:SELECT ID, Name, a.Amount, b.RankFROM MyTable aJOIN (SELECT Amount, RANK () OVER (ORDER BY Amount DESC) as Rank FROM (SELECT DISTINCT Amount FROM MyTable)x) b ON a.Amount = b.AmountORDER BY a.Amount DESC 哪个会给你: Which will give you:IDName AmountRank1AAA 500014SSS 500012BBB 499923CCC 15003 [/ edit] [/edit] 1AAA5000017SSS5000012BBB4999926EEE2800534DDD2800043CCC1500155KKK150006 $ b $bhoặc SELECT *,RANK()OVER(ORDER BY金额DESC) FROM数据 hoặc SELECT *, RANK() OVER(ORDER BY amount DESC) FROM Data1AAA5000017SSS5000012BBB4999936EEE2800544DDD2800053CCC1500165KKK150007 这篇关于如何将最高金额排名为最低金额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-23 08:53