Sql server ROW_NUMBER()& Rank()函数详细信息....它是如何工作的

时间:2012-01-10 14:02:38

标签: sql-server-2005

我从不使用sql server ROW_NUMBER()函数。所以我读了一些关于ROW_NUMBER(),PARTITION& RANK()等,但我还不清楚。

我发现语法就像

SELECT top 10 ROW_NUMBER() OVER(ORDER BY JID DESC) AS 'Row Number',
JID,Specialist,  jobstate, jobtype FROM bbajobs

SELECT top 10 ROW_NUMBER() OVER(PARTITION  BY JID ORDER BY JID DESC) AS 'Row Number',
JID,Specialist,  jobstate, jobtype FROM bbajobs

我有几个问题

1) what over() function does. why we need to specify column name in over function like OVER(ORDER BY JID DESC)

2) i saw sometime people use PARTITION  keyword. what it is?
it is also used in over function like OVER(PARTITION  BY JID ORDER BY JID DESC)
3) in what type of situation we have to use PARTITION  keyword
4) when we specify PARTITION  keyword in over then also we need to specify order by    also why. only PARTITION  keyword can not be used in over clause.
5) what type of situation one should use RANK function
6) what is CTE and what is the advantage of using CTE. it is just like temporary view.
anyone get any performance boost if he/she use CTE other than reusability?

请详细讨论我的观点。如果有人让我理解小而且非常有用,那将会非常有帮助。所有关键字的简单示例,如ROW_NUMBER(),PARTITION&秩()。感谢

2 个答案:

答案 0 :(得分:4)

答案 1 :(得分:1)

  1. 您需要ORDER BY,因为套装没有订单。您需要它用于标准SELECT

  2. PARTITION BY重置每个分区的COUNT

  3. 许多

  4. 请参阅第1点。您可以将PARTITION单独用于SUM,COUNT等

  5. 请参阅MSDN

  6. 单独提问