根据nextN字段,取P列从当前行开始第nextN行的P值和从当前行开始第nextN行的最大值,请问怎么获取?

如表如下,

p   nextN
--- -----
100 1    
101 2    
102 2    
103 1    
104 1    
105 1

根据nextN字段,取P列从当前行开始第nextN行的P值和从当前行开始第nextN行的最大值,结果如下,请问怎么用sql实现?

p   nextN nextNp mmaxnextN
--- ----- ------ ---------
100 1     101    101      
101 2     103    103      
102 3     105    105      
103 1     104    104      
104 1     105    105      
105 1                     
请先 登录 后评论

1 个回答

mhxiang

第nextN行的P值用下标索引,取未来nextN行记录中的最大值可以使用dolphindb的eachRight函数和loop函数实现

p = 100..105
nextN = 1 2 3 1 1 1
tb=select * ,p[ nextN + til(p.size())] as nextNp,eachRight(def(x,range)->x.subarray(range).max(), p, loop(pair, 1..p.size(), 1..p.size() + nextN)) as mmaxnextN from table(p,nextN )
请先 登录 后评论
  • 1 关注
  • 0 收藏,754 浏览
  • haaha 提出于 2022-11-24 11:48

相似问题