SQL2005 取得行号

 

下面介绍一个SQL Server 2005 中的新东东:
ROW_NUMBER
返回结果集分区内行的序列号,每个分区的第一行从 1 开始。

语法:
ROW_NUMBER ( )     OVER ( [ <partition_by_clause> ] <order_by_clause> )

备注:
ORDER BY 子句可确定在特定分区中为行分配唯一 ROW_NUMBER 的顺序。

参数:
<partition_by_clause>
将 FROM 子句生成的结果集划入应用了 ROW_NUMBER 函数的分区。
<order_by_clause>
确定将 ROW_NUMBER 值分配给分区中的行的顺序。

返回类型:
bigint

示例:

 

SELECT * INTO #T
  
FROM(
      
SELECT 2 F1, 'AA' F2, '101' F3 UNION ALL
      
SELECT 1 F1, 'AA' F2, '102' F3 UNION ALL
      
SELECT 4 F1, 'BB' F2, '103' F3 UNION ALL
      
SELECT 5 F1, 'CC' F2, '104' F3 UNION ALL
      
SELECT 3 F1, 'AA' F2, '105' F3 UNION ALL
      
SELECT 7 F1, 'BB' F2, '106' F3 UNION ALL
      
SELECT 6 F1, 'CC' F2, '107' F3 UNION ALL
       
SELECT 5 F1, 'CC' F2, '108' F3
   ) T1

--1
SELECT row_number() OVER (ORDER BY F1 ASC) AS F1Order, * FROM #T
--先按照F1顺序,再加上顺序号F1Order,结果如下:
F1Order               F1           F2    F3
-------------------- ----------- ---- ----
1                    1            AA   102
2                    2            AA   101
3                    3            AA   105
4                    4            BB   103
5                    5            CC   104
6                    5            CC   108
7                    6            CC   107
8                    7            BB   106

--2
SELECT row_number() OVER (ORDER BY F1 DESC) AS F1Order, * FROM #T
--先按照F1倒序,再加上顺序号F1Order,结果如下:
F1Order               F1           F2    F3
-------------------- ----------- ---- ----
1                    7            BB   106
2                    6            CC   107
3                    5            CC   108
4                    5            CC   104
5                    4            BB   103
6                    3            AA   105
7                    2            AA   101
8                    1            AA   102

--3
SELECT row_number() OVER (ORDER BY F1 ASC) AS F1Order, * FROM #T ORDER BY F1 ASC
--先按照F1顺序,再加上顺序号F1Order,再把结果按F1顺序显示,结果如下:
F1Order               F1           F2    F3
-------------------- ----------- ---- ----
1                    1            AA   102
2                    2            AA   101
3                    3            AA   105
4                    4            BB   103
5                    5            CC   104
6                    5            CC   108
7                    6            CC   107
8                    7            BB   106

--4
SELECT row_number() OVER (ORDER BY F1 ASC) AS F1Order, * FROM #T ORDER BY F1 DESC
--先按照F1顺序,再加上顺序号F1Order,再把结果按地F1倒序显示,注意顺序号F1Order也倒过来了,保持位置相对不变,结果如下:
F1Order               F1           F2    F3
-------------------- ----------- ---- ----
8                    7            BB   106
7                    6            CC   107
5                    5            CC   104
6                    5            CC   108
4                    4            BB   103
3                    3            AA   105
2                    2            AA   101
1                    1            AA   102

 

posted @ 2009-08-10 01:56  YaSin  阅读(1204)  评论(0编辑  收藏  举报