简体   繁体   中英

How to get the n-th value and the maximum value of n elements starting from the current position of vector “P“?

I have a table (with column “p” and “nextN”) as follows:

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

I want to obtain a table with two new columns:

  1. take the nextN-th value from the current row of column “P”;

  2. take the maximum value of nextN rows starting from the current row of column “P”;

How can I do with a SQL statement?

You can use the til function to get the nextN-th value from the current row of column “P”, and use function eachRight and loop to get the maximum value of nextN rows starting from the current row of column “P”.

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 )

Output:

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       

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM