WITH tagTab AS( SELECT YearMonth, pm=RANK() OVER(PARTITION BY YearMonth ORDER BY amount DESC) FROM SaleTab ) SELECT * FROM tagTab WHERE pm<=5
開窗函數:sql
http://technet.microsoft.com/zh-cn/library/ms189461(v=sql.105).aspx函數