Highest Close and Lowest Close calculation using SQL.


An example of 50-days Highest close and Lowest close:



select t.d, MAX(t1.c) max50 , MIN(t1.c) 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).


theCrunchR | © 2011