取出每日订单最大金额的前三名ROW_NUMBER


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58
 1,取出所有日期中订单最大金额的前三名
  SELECT * FROM (
    SELECT CONVERT(VARCHAR(10),bo.OrderDate,120) date1, orderno,amount,
    ROW_NUMBER() OVER(ORDER BY bo.amount desc) AS numbers
    FROM bdOrder bo(NOLOCK)
    WHERE bo.OrderDate >='2011-03-19'
)m
WHERE m.numbers <=3
2,若想取出每日的前三名,就需要对数据进行按日期分组,不能用GROUP BY ,因为OVER中有amount,必须要在GROUP BY 中加上amount,也有失去了分组的意向。
    方法:在行级分组,利用强大的OVER,在行级对日期进行分组
    SELECT * FROM (
           SELECT CONVERT(VARCHAR(10),bo.OrderDate,120) date1, orderno,amount,
           ROW_NUMBER() OVER(PARTITION BY CONVERT(VARCHAR(10),bo.OrderDate,120) ORDER BY bo.amount desc) AS numbers
           FROM bdOrder bo(NOLOCK)
           WHERE bo.OrderDate >='2011-03-19'
    )m
     WHERE m.numbers <=3