math - Find the largest sum of three sequential values in SQL? -


say have following table, called revenues.

id | revenue ------------ 1  | 345 2  | 5673 3  | 0 4  | 45 5  | 4134 6  | 35 7  | 533 8  | 856 9  | 636 10 | 35 

i want find largest sum of grouping of sequential 3 values. here's mean:

ids  1 + 2 + 3   =>  345  + 5673 + 0    = 6018 ids  2 + 3 + 4   =>  5673 + 0    + 45   = 5718 ids  3 + 4 + 5   =>  0    + 45   + 4134 = 4179 ids  4 + 5 + 6   =>  45   + 4134 + 35   = 4214 ids  5 + 6 + 7   =>  4134 + 35   + 533  = 4702 ids  6 + 7 + 8   =>  35   + 533  + 856  = 1424 ids  7 + 8 + 9   =>  533  + 856  + 636  = 2025 ids  8 + 9 + 10  =>  856  + 636  + 35   = 1527 

in case, want result 6018, since it's largest sum of 3 sequential values. i'm starting learn sql, other previous language being java, , can think how easy loop. have idea on how started writing query this? similar thing exist in sql?

edit: furthermore, possible scale this? if had big table , wanted find largest sum of hundred sequential values?

one approach use 2 joins id+1 , id+2:

select max(t1.revenue+t2.revenue+t3.revenue) revenues t1 join revenues t2 on t1.id+1 = t2.id join revenues t3 on t1.id+2 = t3.id 

demo.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -