I all ready had this question partially answered by @Gordon Linoff there with was an additional requirements.
I want to find the first row where the subsequent n rows values increment by + 1.
CREATE TABLE #Temp
([ID] int, [cct] int)
;
INSERT INTO #Temp
([ID], [cct])
VALUES
(12807, 6),
(12813, 12),
(12818, 17),
(12823, 22),
(12824, 23),
(12830, 29),
(12831, 30),
(12832, 31),
(12833, 32),
(12835, 34),
(12837, 36),
(12838, 37),
(12839, 38),
(12840, 39),
(12841, 40),
(12844, 43),
(12846, 45),
(12847, 46),
(12848, 47),
(12849, 48),
(12850, 49),
(12851, 50),
(12854, 53),
(12856, 55),
(12857, 56),
(12860, 59),
(12862, 61),
(12863, 62),
(12864, 63),
(12865, 64),
(12866, 65),
(12871, 70),
(12872, 71),
(12873, 72)
;
@Gordon already provided me with this code to find the sequence part of it.
select min(id),min(cct) as cct, count(*) as length
from (select s.*, (cct - row_number() over (order by id)) as grp
from #Temp s
) s
group by grp
having count(*) >= 3
This works perfect to find the sequence. As can be seen from the result.
ID cct length
12830 29 4
12837 36 5
12846 45 6
12862 61 5
12871 70 3
but I need to find the cct value within the sequence that satisfies (cct + 2) % 3 = 0
I need the query to return ID 12838,cct 37 as this is first cct value + 2 divisible by 3 that has the next 2 row values increment by 1.
Any help will be appreciated.
See Question&Answers more detail:
os