Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql server 2005 find series in number

Tags:

sql

sql-server

I have data in following format

89279
89280
89281
89282
89283
89284
89285
89286
89287
89288
160447
160448
160449
160450
160451
160452
160453
160454
160455
160456
160457
160458

i want the o/p in the following format in sql server 2005

89279   89288   10
160447  160458  12
like image 213
arigato gozaimasu Avatar asked Jan 14 '23 00:01

arigato gozaimasu


1 Answers

A standard "gaps and islands" query. SQL Fiddle

WITH T
     AS (SELECT *,
                YourColumn - DENSE_RANK() OVER (ORDER BY YourColumn) AS Grp
         FROM   YourTable)
SELECT MIN(YourColumn),
       MAX(YourColumn),
       COUNT(YourColumn)
FROM   T
GROUP  BY Grp 
like image 196
Martin Smith Avatar answered Jan 16 '23 02:01

Martin Smith