I am trying to do this, as it has to do with matching sequential lines. I am trying to group values ββthat differ by a specific number. For example, let's say I have this table:
CREATE TABLE #TEMP (A int, B int) -- Sample table INSERT INTO #TEMP VALUES (3,1), (3,2), (3,3), (3,4), (5,1), (6,1), (7,2), (8,3), (8,4), (8,5), (8,6) SELECT * FROM #TEMP DROP TABLE #TEMP
And let's say I need to group all the values ββthat differ by 1, having the same value for A. Then I try to get this output:
AB GroupNo 3 1 1 3 2 1 3 3 1 3 4 1 5 1 2 6 1 3 7 2 4 8 3 5 8 4 5 8 5 5 8 6 5
(3,1) (3,2) (3,3) (3,4) and (8,3) (8,4) (8,5) (8,6) were placed in the same group because they differ in value 1. First I will show my attempt:
CREATE TABLE
I will do this on a large table (about 30 million rows), so I was hoping there was a better way to do this for arbitrary values ββ(for example, not only differs by 1, but maybe 2 or 3, which I will later include in the procedure). Any suggestions on whether my approach is a mistake and if it can be improved?