I need to identify missing products who’s numbers are sequential. In other words, there’s a set of products that are sold each with a unique number at each of our stores. The end user wants a report that will identify the missing widgets in a date range.
Store Product Number
Store 1 Widget 100
Store 1 Widget 101
Store 1 Widget 102
Store 1 Widget 104
Store 2 Widget 201
Store 2 Widget 202
Store 2 Widget 203
I thought the best way to do this is to obtain the MAX(Number) and the MIN(Number) and then populate a table variable with all of the numbers in the sequence. If the Widget Number from the table variable doesn’t exist in the Product table, I would return the number and mark it as missing.
However; our DBA is very much against Cursors and I know WHILE loops take a lot of overhead. Plus, I’m not sure how to do this on a by store basis.
Does anyone know of the way to do this in a set based manner?
The widgets themselves have numbers, so when you sell widget #1, the next widget you sell should be widget #2. However; sometimes, that doesn’t happen and widget #3 is sold – widget #2 is missing and not in the database. I need to identify on a report that widgets #1 and #3 were sold and #2 is missing.
Updated Answer
Sample code to create a table and working solution are below. Basically you do 2
EXISTSchecks – to see if there are numbers higher than the current number, and there is not a number one higher than the current number.