Actually, this is not specific to SQL, and I doubt "conversation pattern" is the correct name, but I could not think of a better caption.
To simplify, imagine you got a vast stream of ints. The task is to detect an A.{1;max_n}A
pattern: An int satisfies the pattern if it is followed by n (> 0) other ints, then the original int again, while n <= max_n.
Example:
...
1
4 <--
7 \
3 > n = 3
3 /
4 <--
2
...
Here, the int 4
is repeated with 3 arbitrary ints in-between, so for max_n <= 3 the pattern is satisfied for the value 4
.
The question is, how can I detect which integers in the huge dump of data follow this pattern? I'm mostly interested in the algorithm itself, but an example in SQL or C# would be welcome, too.
The naive idea I came up with is to first gather a list or all distinct ints, then check the pattern in a straightforward way for each of them, but that would lead to a performance bottleneck.