与SQLserver相加的数字序列

时间:2013-07-02 08:17:58

标签: sql sql-server sql-server-2005

参考我的previous问题,我是否可以再添加一列:

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

并且有o / p喜欢

89279   89288   10 500
160447  160458  12 600

1 个答案:

答案 0 :(得分:2)

试试这个

WITH T
AS
(
  SELECT *,
   col1 - DENSE_RANK() OVER (ORDER BY col1) AS Grp
  FROM   Table1
)
SELECT 
    MIN(col1)
   ,MAX(col1)
   ,COUNT(col1)
   ,SUM(col2)
FROM   T
GROUP  BY Grp 

编辑马丁帖子:Sql server 2005 find series in number

SQL FIDDLE DEMO