How to count consecutive row where one column have a specific value in SQL Server? <p>I have a data set that looks like this</p> <pre><code>gId mId 226 88825 226 88825 226 88825 226 88825 226 88825 226 88825 226 88832 226 88832 226 88832 226 88832 226 88863 226 88863 226 88863 226 88863 226 88863 227 89080 227 89080 227 89080 227 89148 227 89148 227 89148 227 89197 227 89197 227 89197 227 89148 227 89197 227 89197 227 89197 227 89197 227 89148 227 89197 227 89197 227 89197 227 89197 227 89148 227 89197 227 89197 227 89197 229 89267 229 89318 229 89322 231 90257 231 90340 231 90350 247 94318 247 94318 249 94642 249 94642 249 94642 249 94400 249 94642 249 94642 249 94642 249 94642 249 94642 249 94642 249 94400 249 94400 249 94400 249 94400 </code></pre> <p>I need to be able to get a list of the unique <code>gId</code> column where the <code>mId</code> column contains the same value in 5 or more consecutive rows. So the above data set will return something like this</p> <pre><code>gId mId 226 88825 226 88863 249 94642 </code></pre> <p>One important thing to mention that I can't change the order of this list so I have to count it top to bottom to be able to check for consecutive rows.</p> <p>My table looks like this</p> <pre><code>CREATE TABLE t ( gId int NOT NULL, mId int NOT NULL ); </code></pre>