0

I have a Select statement that pulls customer information i.e. CustomerID, FirstName, LastName, etc.

I want to be able to assign a sales person to the best 100 customers ranked by sales.

I have the select statement set up to pull the best 100 customers, how would I add a column to the statement that numbers the customers in such a way:

SalesID 
1, 2, 3, 4, 1, 2, 3, 4...

Where my sales people ID is repeated over and over for all records.

1 Answer 1

1

You can use row_number() and modulus arithmetic:

select t.*,
       1 + (row_number() over (order by . . . ) % 4)
from t
order by . . .

The . . . is for the ordering columns that specify "best". This should be the same for both order by clauses.

Note that some databases use mod() instead of %.

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.