"C.F. Scheidecker Antunes" <[EMAIL PROTECTED]> wrote:

>I have a table that has a PartNumber and the Quantity of Items sold for 
>each partNumber, that is:
>
>PartNumber
>Qty
>
>I need to get its sales raking given its PartNumber, that is. So if I 
>order the table by Qyt in descending order the first
>record will be the partNumber that sold the most. If I want to know what 
>is 123 raking position according to that.
>
>Is there any easy way to do it?

I think you may be looking for something like this:

set @a = 0;
select @a := @a+1 as rank, PartNumber from my_table order by Qty desc;

Unfortunately, selecting only the record with rank 123 doesn't work very
well with this query, so you may want to load a temporary table and then
select from that table:

set @a = 0;
create temporary table foo
 select @a := @a+1 as rank, PartNumber from my_table order by Qty desc;
select * from foo where rank = 123;

(I haven't addressed the question of how you deal with records where
the quantity is the same.  Left as an exercise for the reader. :-)

 - seb


-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to