Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this</p> <pre><code>WITH cte AS ( SELECT *,COUNT(1) OVER(PARTITION BY cnt) pt FROM ( SELECT tt.* ,(SELECT COUNT(id) FROM t WHERE data &lt;= 10 AND ID &lt; tt.ID) AS cnt FROM t tt WHERE data &gt; 10 ) t1 ) SELECT id, [when], data FROM cte WHERE pt &gt;= 3 </code></pre> <p><a href="http://www.sqlfiddle.com/#!3/2e5f4/1" rel="nofollow noreferrer"><strong>SQL FIDDLE DEMO</strong></a></p> <p><strong>OUTPUT</strong></p> <pre><code>id when data 2 2013-08-02 00:00:00.000 121 3 2013-08-03 00:00:00.000 132 4 2013-08-04 00:00:00.000 15 6 2013-08-06 00:00:00.000 1435 7 2013-08-07 00:00:00.000 143 8 2013-08-08 00:00:00.000 18 9 2013-08-09 00:00:00.000 19 </code></pre> <p><strong>EDIT</strong></p> <p>First the inner query counts the no of records where data &lt;= 10</p> <pre><code>SELECT tt.* ,(SELECT COUNT(id) FROM t WHERE data &lt;= 10 AND ID &lt; tt.ID) AS cnt FROM t tt </code></pre> <p>output </p> <pre><code>id when data cnt 1 2013-08-01 00:00:00.000 1 1 2 2013-08-02 00:00:00.000 121 1 3 2013-08-03 00:00:00.000 132 1 4 2013-08-04 00:00:00.000 15 1 5 2013-08-05 00:00:00.000 9 2 6 2013-08-06 00:00:00.000 1435 2 7 2013-08-07 00:00:00.000 143 2 8 2013-08-08 00:00:00.000 18 2 9 2013-08-09 00:00:00.000 19 2 10 2013-08-10 00:00:00.000 1 3 11 2013-08-11 00:00:00.000 1234 3 12 2013-08-12 00:00:00.000 124 3 13 2013-08-13 00:00:00.000 6 4 </code></pre> <p>Then we filter the records with data > 10 </p> <pre><code>WHERE data &gt; 10 </code></pre> <p>Now we count the records by partitoning cnt column</p> <pre><code>SELECT *,COUNT(1) OVER(PARTITION BY cnt) pt FROM ( SELECT tt.* ,(SELECT COUNT(id) FROM t WHERE data &lt;= 10 AND ID &lt; tt.ID) AS cnt FROM t tt WHERE data &gt; 10 ) t1 </code></pre> <p>Output</p> <pre><code>id when data cnt pt 2 2013-08-02 00:00:00.000 121 1 3 3 2013-08-03 00:00:00.000 132 1 3 4 2013-08-04 00:00:00.000 15 1 3 6 2013-08-06 00:00:00.000 1435 2 4 7 2013-08-07 00:00:00.000 143 2 4 8 2013-08-08 00:00:00.000 18 2 4 9 2013-08-09 00:00:00.000 19 2 4 11 2013-08-11 00:00:00.000 1234 3 2 12 2013-08-12 00:00:00.000 124 3 2 </code></pre> <p>The above query is put in cte just like temp table</p> <p>Now select the records that are having the consecutive count >= 3</p> <pre><code>SELECT id, [when], data FROM cte WHERE pt &gt;= 3 </code></pre> <p><strong>ANOTHER SOLUTION</strong></p> <pre><code>;WITH partitioned AS ( SELECT *, id - ROW_NUMBER() OVER (ORDER BY id) AS grp FROM t WHERE data &gt; 10 ), counted AS ( SELECT *, COUNT(*) OVER (PARTITION BY grp) AS cnt FROM partitioned ) SELECT id, [when], data FROM counted WHERE cnt &gt;= 3 </code></pre> <p><a href="https://dba.stackexchange.com/questions/36943/find-n-consecutive-free-numbers-from-table"><strong>Reference URL</strong></a></p> <p><a href="http://www.sqlfiddle.com/#!3/2e5f4/8" rel="nofollow noreferrer"><strong>SQL FIDDLE DEMO</strong></a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload