1)
Select TOP 1 WITH TIES MIN([close]) AS LowestPrice, [year], [month]
FROM @tab
GROUP BY [year], [month]
ORDER BY LowestPrice
2)Find which stocks have shown the biggest gain in closing price over the period of January 1, 2007 to November 1, 2012? (the change in closing price from the first day to the last day)
;with cte as
(
SELECT Sector, Symbol, [Close], ROW_NUMBER() OVER(PARTITION by symbol ORDER BY [year], DATEPART(mm,CAST([month]+ ' 1900' AS DATETIME)), [day]) rn
FROM @tab
WHERE CAST ( cast([year] AS CHAR(4)) + ' ' + [month] + ' ' + CAST([day] AS VARCHAR(2)) AS DATE) BETWEEN '2007-01-01' AND '2012-11-01'
)
SELECT TOP 1 WITH TIES t2.Symbol, t1.[Close] - t2.[Close] maxDiff
FROM cte t1
JOIN cte t2 ON t1.rn = t2.rn+1 and t1.Symbol = t2.Symbol
ORDER BY maxDiff DESC
--
Chandu