How do I write a SQL statement that would arrange a column of serial numbers so that they are in groups with a range of 1000 or less (based on their serial numbers)? The numbers are not currently sorted but would end up like the example below. There are only two numbers in the first example group (having a difference of 31). The third number has a difference of 6443 from the second number placing it in a separate grouping (>1000). The next 4 numbers are grouped based on their range of …4015 to …4865 (diff of 850).
I’m still a noob with < 6 months of TSQL. I don’t even know where to start with this one.
serial_num
----------
33XG547909
33XG547940
33XG554383
33XG564015
33XG564282
33XG564289
33XG564308
33XG564314
33XG564353
33XG564865
33XG569023
34LT242788
34LT242812
If you’re looking to group items in a sliding scale where each item can be grouped with the thousand nearest items, you’ll have a lot of trouble since there would be so many combinations. They probably wouldn’t be helpful either unless you’re looking at a specific item and trying to find the thousand closest for just that one item.
I think Astander has a good solution, but it doesn’t cover the starting portion of the serial. It looks like your solution is a simple as:
With the results being