I have my database with three tables:

The order table has the following data:
OrderID OperatorID GroupID OrderDesc Status Cash ... -------------------------------------------------------------------------- 1 1 1 small order 1 100 2 1 1 another order 2 0 3 1 2 xxxxxxxxxxx 2 1000 5 2 2 yyyyyyyyyyy 2 150 9 5 1 xxxxxxxxxxx 1 0 10 NULL 2 xxxxxxxxxxx 1 10 11 NULL 3 xxxxxxxxxxx 1 120
Operator table:
OperatorID Name GroupID Active
Group table:
GroupID Name --------------- 1 G1 2 G2 3 X1
As you can see, John has 3 warrants, Kate 1, Will 1, Jack and Sam not.
Now I would like to assign operators to the order base in some conditions:
- order must have cash> 0
- order must have status = 1
- order must be in group 1 or 2 Operator
- must be active (active = 1)
- must be in group 1 or 2
This is the result I would like to get:
OrderID OperatorID GroupID OrderDesc Status Cash ... -------------------------------------------------------------------------- 1 1 1 small order 1 100 < change 2 1 1 another order 2 0 3 2 2 xxxxxxxxxxx 2 1000 < change 5 4 2 yyyyyyyyyyy 2 150 < change 9 5 1 xxxxxxxxxxx 1 0 10 4 2 xxxxxxxxxxx 1 10 < change 11 NULL 3 xxxxxxxxxxx 1 120
I would like to shuffle orders and update the operator identifier so that every time I call this script, I get a random recipient operator identifier, but each operator will have an equal number or orders (close to equal, because if I have There are 7 orders; one person will have 3 and the remaining 2).
I can use NTILE to distribute orders among groups, but I need to assign an operatorID to this group.
I think I need to do something like this:
SELECT NTILE(2) OVER( order by orderID desc) as newID,* FROM orders(NOLOCK)
This will give me an order table grouped in equal parts. I need to know the length of the statement table (to add it as a parameter in NTILE), after which I could join my results with statements (using row_number() )
Is there a better solution?
My question is again: How to equally divide the result set into groups and update this record set using other table data?
EDIT: This is my code: http://sqlfiddle.com/#!3/39849/25
EDIT 2 I updated my question and added additional conditions.
I would like to assign operators to orders based on some conditions:
- order must have cash> 0
- order must have status = 1
- order must be in group 1 or 2 Operator
- must be active (active = 1)
- must be in group 1 or 2
I create this request as a stored procedure.
Thus, the first step is to create data with new assignments in the temporary table and after final approval in the second stage to update the main table based on this temporary table.
I have two more questions:
Would it be better to first select all all orders and all operators that satisfy the conditions in the temporary table, and then perform shuffling or do all this in one big query?
I would like to pass an array or groups as a parameter to my procedure. Which option is best to pass an array to a stored procedure (SQL Server 2005).
I know that this has been asked many times, but I would like to know whether it is better to create a separate function that will cut a comma into a table ( http://www.sommarskog.se/arrays-in-sql-2005.html ) or put all in one big fat procedure? :)
FINAL ANSWER: avilable at http://sqlfiddle.com/#!3/afb48/2
SELECT o.*, op.operatorName AS NewOperator, op.operatorID AS NewOperatorId FROM (SELECT o.*, (ROW_NUMBER() over (ORDER BY newid()) % numoperators) + 1 AS randseqnum FROM Orders o CROSS JOIN (SELECT COUNT(*) AS numoperators FROM operators WHERE operators.active=1) op WHERE o.cash>0 and o.status in (1,3) ) o JOIN (SELECT op.*, ROW_NUMBER() over (ORDER BY newid()) AS seqnum FROM Operators op WHERE op.active=1 ) op ON o.randseqnum = op.seqnum ORDER BY o.orderID
Answer based on the answer of Gordon Linoff. Thanks!