SQL使用开窗函数与CTE查询每月销售额的前几名

WITH tagTab AS(
SELECT YearMonth, 
pm=RANK() OVER(PARTITION BY YearMonth ORDER BY amount DESC) 
FROM SaleTab
)
SELECT * FROM tagTab WHERE pm<=5

 开窗函数:

http://technet.microsoft.com/zh-cn/library/ms189461(v=sql.105).aspx

 

你可能感兴趣的:(sql)