2017-10-21 35 views
0
Date  column1 column2 column3 column4 column5 
01-Jan-17 A  AB  10  AB_1 10 
02-Jan-17 B  AB  20  AB_2 10 
03-Jan-17 C  AB  30  AB_3 10 
04-Jan-17 D  AB  20  AB_4 -10 
05-Jan-17 E  AB  40  AB_5 20 
06-Jan-17 X  GH  30  GH_1 30 
07-Jan-17 V  GH  40  GH_2 10 
08-Jan-17 A  GH  50  GH_3 10 

Requirement1:对于具有在列2相同的值的所有列,则column4应当按顺序编号的Sybase +如何计算序列的编号为speicific组

Requirement2:对于具有在列2相同值的所有列,第5列应计算为第3列的当前值 - 第3列的前一值

感谢您的帮助!

我使用的Adaptive Server Enterprise/15.7/EBF 22234 SMP SP121/P/x86_64的/企业版Linux/ase157sp12x /六十四分之三千六百六十位/ FBO/

+0

到目前为止您尝试过哪些查询?哪些Sybase产品(ASE,SQLAnywhere,IQ,Advantage)和哪个版本? – markp

+0

你想对数据做什么?更新表格?或者只是运行一个可以对行进行排序的选择? –

+0

它看起来(对我而言)就像您发布的数据是期望的结果集;如果是这种情况......原始数据是什么样的?我建议你看一下[如何创建一个最小的,完整的和可验证的例子](https://stackoverflow.com/help/mcve),然后回来并更新你的问题,并提供必要的细节来澄清你是什么'试图做 – markp

回答

0

你可以用多步处理,实现这一目标跟随(我没有;测试它,但你会明白)。假设你的表名为'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) 
+0

真棒@RobV !!!!实际上它可以通过使用seq_no = id - (从#t2中选择min(id),其中tv2.column2 = tv1.column2)+1进一步缩小。另外,最后一个sql不起作用,而我已经使用了和(t2。 seq_no-t1.seq_no)= 1 ..总的来说你的想法很棒!非常感谢 !!!! – user2186469