日期column1 column2 column3 column4 column501-Jan-17 A AB 10 AB_1 1002-Jan-17 B AB 20 AB_2 1003-Jan-17 C AB 30 AB_3 1004年1月4日D …
您可以通过如下多步骤处理来实现这一点(我没有测试它,但你会得到这个想法)。假设你的表名为't'。
select your_date, column1, column2, column3, id = identity(9) into #t1 from t order by column2, column3 -- this seems to be the ordering you want? select column2, min(id) as min_id into #t2 from #t1 group by column2 select #t1.* column4 = (#t1.id - #t2.min_id + 1) into #t3 from #t1, #t2 where #t2.column2 = #t1.column2 select ta.*, column5 = case when tb.id is null or tb.column2 <> ta.column2 then ta.column3 else ta.column3 - tb.column3 from #t3 ta, #t1 tb where ta.id *= (tb.id - 1)