Highest High or Lowes Low calculation using SQL.


An example of 50-days High and Low:



select t.d, MAX(t1.h) max50 , MIN(t1.l) min50
from tb t 
cross tb t1
where t1.Id <= t.Id
and  t1.id > t.id - 50 
and t.d = '1/25/2011'
group by  t.d 

Table structure: id (consecurive integer), d (date),c (closing price), h (day's high), l (day's low).


theCrunchR | © 2011