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:
take the nextN-th value from the current row of column “P”;
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.