Netflix Style Queue Priority

Netflix Style Queue Priority

I am trying to figure something out and hitting a brick wall. If I had some code I would post it, but I just can't figure out how to even start. I’ve searched but not finding what I need.

Each of our manufacturing lines can have multiple Work orders assigned to it. 1 is the highest priority, 2 will be the next Work Order to be produced on that line. There are 20 lines. I don't currently have a timestamp on the table but I think I will need to and can add one.

The supervisor sets the line and priority for each Work Order. The supervisor also closes the work orders. When the Work Order for Line 1, Priority 1 is closed I need the Work Order currently set to Priority 2 for that same line to now be Priority 1, and so on throughout the rest of the Work Orders on that line.

To further complicate this. Sometimes the supervisor will set the Work Order having Priority 3 up to the front of the line, making it priority 1. I also would then need to re-arrange the rest of the Work Orders back by 1, making the current 1 priority 2, the current 2 priority 3 etc.

I should have given more detail on my original posting, at the very least some code to show I am trying. I wish I had. Hopefully this will help get a response.

This code gets the data into the correct order. I just don't know how to re-order them.

Line is where the work order is to be done. We currently run 16 lines. Priority is the order in which all work orders on that line will be performed. I put the timestamp in because that will help to get the work orders in the proper order should 2 work orders on the same line have the same priority, the most recent timestamp should get done first.

I don't really know how to use a cursor. I have heard many grumblings about staying away from them, but I am really at a loss.

You haven't really made it clear what you want to do...I will explain why.

You started by saying that someone sets the priorities, and that they can move an order from priority 3 to 1 (for example) and this should make the other orders shift down the queue.

Then you provided an example where you set an order to priority 1 (we don't know what priority it was before) but you do not require the other orders to shift down (alter priority)...

This is contradictory without further detail.

If the supervisor makes an order move from priority 3 to 1 and there are already some orders at priority 1, where in the queue does it go? Perhaps she has the option to jump to the top of a priority group and bump the existing orders of that priority and below "down one level"? Perhaps she also has the option to merge with an existing group thereby causing no bump?

Thank you so much for taking the time to reply and pointing me into the direction of the boards etiquette. It was very helpful.

There can only be one work order on a line at a time. If the supervisor sets a Work Order to 1 where there is already an existing work order on that line with a 1 priority, then all other work orders on that line would bump down by 1. (Example 1 below)

If the work order in priority 1 gets its priority set to 4, then the existing work orders set to 2, 3, 4 would bump up to 1, 2, 3 respectively. (Example 2 below)

If the work order in priority 1 gets its priority set to 3, then the existing work orders set to 2, 3 would bump up to 1, 2 respectively. The work order in priority 4 would not change. (Example 3 below)

The most difficult scenarios involve when a work order moves from one line to another. This one Work Order will affect 2 separate lines. Work Order with Priority 1 moves from Line 3 to Line 4 and it's new priority is 2. Priorities 2, 3, 4 would need to bump up to 1, 2, 3. The moved Work Order would get priority 2 on line 4, Priorities 3, 4 would get bumped down to 4, 5. (Example 4 below)

Probably doesn't need to be said, but with the examples below please only un-comment one of them at a time.

I do really appreciate your help not only with my query but the board also. Thanks Again.

Here's the code...

--===== If the test table already exists, drop it IF OBJECT_ID('TempDB..#FPD_Schedule','U') IS NOT NULL DROP TABLE #FPD_Schedule

-- collect the existing data for the keyDECLARE @old_priority INT,@old_line INTSELECT @old_priority=priority,@old_line=line FROM FPD_Schedule WHERE [KEY]=@key

-- move everything up on the line we are moving from-- move everything down on the line we are moving to-- move the specified orderUPDATE FPD_ScheduleSET priority= CASE [KEY] WHEN @key THEN @priority ELSE CASE WHEN @line=@old_line THEN priority + CASE WHEN priority BETWEEN @priority AND @old_priority OR priority BETWEEN @old_priority AND @priority THEN CASE WHEN @priority<@old_priority THEN 1 WHEN @priority>@old_priority THEN -1 ELSE 0 END ELSE 0 END WHEN line=@old_line AND priority>@old_priority THEN priority-1 WHEN line=@line AND priority>=@priority THEN priority+1 ELSE priority END END , line = CASE [KEY] WHEN @key THEN @line ELSE [line] ENDWHERE line=@line OR line=@old_line

It looks a bit horrific but that is just because I wanted to perform all updates in one statement - the alternative being to read /update the table two or three times...which I wanted to avoid.

I just re-copied your sp and it is working like a charm. For the life of I don't know what I did to mess it up.

The code does confuse me though. I am having a difficult time following the thought process. I hate to burden you, but if you have some time to explain the code a little, I'm sure I'd learn more about it. If not, I'll just tear it apart next week at the office.

Skip (7/4/2010)I just re-copied your sp and it is working like a charm. For the life of I don't know what I did to mess it up.

The code does confuse me though. I am having a difficult time following the thought process. I hate to burden you, but if you have some time to explain the code a little, I'm sure I'd learn more about it. If not, I'll just tear it apart next week at the office.

-- collect the existing data for the keyDECLARE @old_priority INT,@old_line INTSELECT @old_priority=priority,@old_line=line FROM FPD_Schedule WHERE [KEY]=@key

-- move everything up on the line we are moving from-- move everything down on the line we are moving to-- move the specified orderUPDATE FPD_Schedule

So, this one update will take care of all three main categories of rows

Items in the line which originally held the order

Items in the line to which the order will move

The order which is moving

First we can handle the order we are moving by checking for the supplied @Key value in the key field and setting the required priority

SET priority= CASE [KEY] WHEN @key THEN @priority

Next we handle the case where we are just changing priority on the same line:

ELSE CASE WHEN @line=@old_line THEN priority +

We need to either increment or decrement the priority by 1 for all orders which have a priority that lies between the original priority of the order we are moving and it's new priority.

Suppose we move priority 4 to priority 2 then we need to adjust everything between priority 2 and 4

1 2* 3* 4*

This CASE statement finds orders in that priority range

CASE WHEN priority BETWEEN @priority AND @old_priority OR priority BETWEEN @old_priority AND @priority THEN

And this one says that is we are moving the order up the line then all other orders in the selected range must move down (+1) and vice versa (-1)(and if the priority of the selected order is not changing, then don't move anything (0))

CASE WHEN @priority<@old_priority THEN 1 WHEN @priority>@old_priority THEN -1 ELSE 0 END

And this ELSE is for orders that are not affected by the change, so use an adjustment value of zero.

ELSE 0 END

Next, we handle where the order is changing line.

First, move the priority of orders on the old line up one.

WHEN line=@old_line AND priority>@old_priority THEN priority-1

Then move the orders on the new line down one.

WHEN line=@line AND priority>=@priority THEN priority+1

And any orders that are not affected by the order move keep their original priority....

ELSE priority END END

And finally, move the selected order to it's new line (even if that is the same as the old line - no harm done.)

, line = CASE [KEY] WHEN @key THEN @line ELSE [line] END

And select only the lines we are affecting...

WHERE line=@line OR line=@old_line

You could also extend the where clause to be more selective by only selecting records from each line that will be affected (using the priority range) but it's late and I don't have time to do that now....