insert into test values('A', null, 1);insert into test values('A', null, 2);insert into test values('A', null, 3);insert into test values('B', null, 4);insert into test values('B', null, 10);insert into test values('A', null, 11);insert into test values('A', null, 15);insert into test values('B', null, 16);insert into test values('C', null, 20);insert into test values('C', null, 21);insert into test values('A', null, 22);insert into test values('A', null, 23);
The field "position" is unique, no two rows contain the same value. There may however be gaps in the number list (see above)
Now what I'd like to do with TSQL is to identify the "groupings" of sequences of the "position" number, for each name.
The name "A" first sequence group is for position 1,2,3 - its sequence 2 is for position 11,15 and its sequence 3 is for position 22,23
The "B" name sequence group 1 contains position 4,10 and its sequence 2 is for position 16
And the "C" name has only got one sequence group, for position 20,21
Thus, I'd like to the result of the TSQL update be identical to if I had done the following insert to begin with:
insert into test values('A', 1, 1);insert into test values('A', 1, 2);insert into test values('A', 1, 3);insert into test values('B', 1, 4);insert into test values('B', 1, 10);insert into test values('A', 2, 11);insert into test values('A', 2, 15);insert into test values('B', 2, 16);insert into test values('C', 1, 20);insert into test values('C', 1, 21);insert into test values('A', 3, 22);insert into test values('A', 3, 23);
the only thing you will need to add, over the article suggestion, is in the row_number() functions to add this:
row_number() over ( partition by name order by position )
And @emoreau, your suggestion kind of works out-of-the-box as well, almost - perfect.
I'll just have to get that T number to go sequentially 1,2,3,... for each "name".
Well the methods given above will NOT scale when data will grow..
Use below method. It will improve the performance heavily.
SELECT Name, StartPosition, EndPosition FROM ( SELECT Name,LeadValue, LagValue,position StartPosition, CASE (LEAD(name) OVER (ORDER BY ( select 1))) WHEN name THEN LEAD(position) OVER (ORDER BY ( select 1)) ELSE position END EndPosition FROM ( SELECT name ,CASE (LAG(name) OVER (ORDER BY ( select 1 ))) WHEN name THEN 1 ELSE 0 END LagValue ,CASE (LEAD(name) OVER (ORDER BY ( select 1))) WHEN name THEN 1 ELSE 0 END LeadValue ,position FROM test ) tbl2 WHERE tbl2.LagValue = 0 OR tbl2.LeadValue = 0) tbl3 WHERE LagValue=0
row_number() over ( partition by name order by position )
once you create a "staging table" with the proper values, you can then use this other article I wrote to update the values as needed:
https://www.experts-exchange.com/articles/1517/UPDATES-with-JOIN-for-everybody.html