Skip to content
BytePatterns

Same Reading Three Times in a Row

MediumSQL#self-join#consecutive-rows#distinct~20m

Problem

A sensor appends one row per reading to reading(id, value). The id column counts up by exactly 1 per row with no gaps, so it records the order the readings arrived in. Write a query that returns every value that was read at least three times in a row, as a single column value, sorted, with each value listed once.

Examples

Input:  reading = [(1, 1), (2, 1), (3, 1), (4, 2), (5, 1), (6, 2), (7, 2)]
Output: [(1,)]
Why:    rows 1 to 3 all read 1; 2 appears three times but never three rows in a row
Input:  reading = [(1, 7), (2, 7), (3, 7), (4, 7), (5, 3), (6, 3), (7, 3)]
Output: [(3,), (7,)]
Why:    a run of four 7s contains two runs of three, but 7 is still listed once
Input:  reading = [(1, 5), (2, 5)]
Output: []
Why:    edge case, two equal rows are not enough

Hints

0 / 3

Stuck on the idea rather than the code? INNER and OUTER JOINs covers it.