答案 0 :(得分:1)
您可以使用交叉申请查找最大和最小行,并使用dense_rank对您进行分组。
SQL更新感谢Tony简化我的交叉申请;)
declare @myt table (id int,number bigint, tip int, kalaf int)
insert into @myt
values
(971545701,4110897922,411,41108979),
(971578550,6131339133,613,61313391),
(971578992,6131339402,613,61313394),
(971578993,6131339403,613,61313394),
(971578994,6131339404,613,61313394),
(971579095,6131339627,613,61313396),
(971579100,6131339632,613,61313396),
(971579102,6131339634,613,61313396);
WITH T
AS (
SELECT *
,DENSE_RANK() OVER (
ORDER BY ID
) - ID AS Grp
FROM @myt
)
,test
AS (
SELECT MIN(ID) AS RangeStart
,MAX(ID) AS RangeEnd
,MIN(number) AS minNum
,MAX(number) AS maxNum
FROM T
GROUP BY Grp
)
SELECT t.RangeStart AS ID
,t.minNum
,t.maxNum
,f.tip
,f.kalaf
FROM test t
INNER JOIN @myt f ON t.RangeStart = f.id
Order by ID
<强>结果强>