4

我有以下格式的数据

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

我想要 sql server 2005 中以下格式的 o/p

89279   89288   10
160447  160458  12
4

1 回答 1

9

一个标准的“差距和孤岛”查询。 SQL小提琴

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 
于 2013-07-02T06:46:46.277 回答