如何在“值”列中获取“继续”数据的“开始”和“结束ID”?

时间:2016-09-15 12:18:12

标签: mysql sql sql-server sql-server-2008 oracle11g

您好我在表格中有如下数据:

Source Table

我正在寻找以下输出,

Output

我正在使用SQL Server 2008 R2。 我只是尝试自我加入表ID1 = ID2 + 1和值不等于条件。

Select    T1.ID as ID1 ,T1.Value V1 ,T2.ID as ID2, T2.Value as V2 From [Test_Seq] T1 
                INNER Join [Test_Seq] T2 
                 on T2.ID = T1.ID+1  
                 Where T1.Value <>T2.Value

它给我低于输出,

Result Set

我希望我能在这里得到更好的答案,并对这种情况做出明确的解释。

由于

2 个答案:

答案 0 :(得分:1)

您可以尝试使用此查询

;with cte as (
select *, row_number() over(partition by value order by id) as rn1,  row_number() over(order by id) rn from #startend
) , cte2 as (
select min(id) startid, max(id) as endid, value, rn-rn1 as r from cte
group by rn-rn1, value
) select startid, endid, value from cte2
order by startid

答案 1 :(得分:0)

ConvertShapeToPic()