-1

I need to calculate the rank in mysql. Suppose I have list of sum of my product sales values of entire month then i need to rank the product from highest sales value in order to rank like 1 ,2 ,3 etc

Month  Product  Sum of Sales

Jan     Latop       450000
jan     Latop       150000
Jan     Latop       250000
Feb     Desktop     200000
Feb     Desktop     150000
Feb     Desktop     180000

so from above data output will be like

Month Product Sum of Sales rank

Jan     Latop       450000  1
Jan     Latop       250000  2
jan     Latop       150000  3
Feb     Desktop     200000  1
Feb     Desktop     180000  2
Feb     Desktop     150000  3
Sdesh
  • 55
  • 9
  • 2
    totally not clear. Where are data? Wanted behaviour? What is your attempt? – Jacek Cz Sep 07 '17 at 06:26
  • i have put the data above but its converted into text format, i just wat to use the rank function which is not available in mysql – Sdesh Sep 07 '17 at 06:30
  • How would you handle ties? What would the desired result look like? And see https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query – Strawberry Sep 07 '17 at 07:34
  • this is one of the most frequently asked questions for `mysql` tag – newtover Sep 07 '17 at 07:35
  • Possible duplicate of [Rank function in MySQL](https://stackoverflow.com/questions/3333665/rank-function-in-mysql) – newtover Sep 07 '17 at 07:35
  • Post your query which calculates this sum – M Khalid Junaid Dec 28 '17 at 11:04
  • sdesh as @newtover pointed out it's duplicate question. Look here for ready answer: https://stackoverflow.com/a/25763224/330242 What you need is ranking over partition – przemo_li Dec 28 '17 at 11:26

2 Answers2

0

You can do something like this:

SELECT month,product,sumOfSales, @curRank := @curRank + 1 AS rank
FROM products p, (
SELECT @curRank := 0
) q
ORDER BY sumOfSales DESC;

I am assuming the table name is product and column name is sumOfSales.

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
-1

You Can use this Query SELECT sales FROM TABLE Order by sales DESC sales is your column name where sum of sale is stored Query Will Return Record with most sales first and so on.

Adil Amanat
  • 70
  • 1
  • 5