3

I'm have a database customer name and date are the composite keys. I'm trying to pull out the 'x' largest or smallest transactions on each date and insert it into another table along with the ranking.

I'm using this code on individual runs, but it won't work when run thru JDBC from java for multiple runs.

set @N = 0;

SELECT @N := @N +1 AS rank, name, purchase FROM t1 ORDER BY close purchase
limit 15;

Any suggestions?

2
  • 2
    define it won't work? errors? incorrect behavior? does it work for the first run and fail on successive runs? or all runs? Commented Oct 7, 2011 at 16:00
  • Are stored procedures an option? Commented Oct 7, 2011 at 16:03

1 Answer 1

5

This works perfectly:

SELECT t.*, 
       @n := @n + 1 AS rank
  FROM WhateverTable t, (SELECT @n := 0) r

So your query will actually look like this:

SELECT @N := @N +1 AS rank, name, purchase 
FROM t1, (SELECT @N := 0) r 
ORDER BY purchase desc
limit 15;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.