sql2005排序函数

 

  排名函数是 SQL Server2005新加的功能。在 SQL Server2005中有如下四个排名函数:

1. row_number

2. rank

3. dense_rank

4. ntile   
    下面分别介绍一下这四个排名函数的功能及用法。在介绍之前假设有一个t_table表,表结构与表中的数据如图1所示:



图1
<!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->

其中 field1字段的类型是 int field2字段的类型是 varchar

一、 row_number

    row_number函数的用途是非常广泛,这个函数的功能是为查询出来的每一行记录生成一个序号。 row_number函数的用法如下面的 SQL语句所示:

 

select  row_number()  over ( order   by  field1)  as  row_number, *   from  t_table

    上面的 SQL语句的查询结果如图 2所示。



图2
<!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->

    其中 row_number列是由 row_number函数生成的序号列。在使用 row_number函数是要使用 over子句选择对某一列进行排序,然后才能生成序号。

    实际上, row_number函数生成序号的基本原理是先使用 over子句中的排序语句对记录进行排序,然后按着这个顺序生成序号。 over子句中的 order by子句与 SQL语句中的 order by子句没有任何关系,这两处的 order by 可以完全不同,如下面的 SQL语句所示:

 

select  row_number()  over ( order   by  field2  desc as  row_number, *   from  t_table  order   by  field1  desc

    上面的 SQL语句的查询结果如图 3所示。



图3
<!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->     我们可以使用row_number函数来实现查询表中指定范围的记录,一般将其应用到Web应用程序的分页功能上。下面的SQL语句可以查询t_table表中第2条和第3条记录:

 

with  t_rowtable
as
(
    
select  row_number()  over ( order   by  field1)  as  row_number, *   from  t_table
)
select   *   from  t_rowtable  where  row_number > 1   and  row_number  <   4   order   by  field1

<!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->     上面的SQL语句的查询结果如图4所示。



图4
<!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->     上面的 SQL语句使用了 CTE,关于 CTE的介绍将读者参阅 《SQL Server2005杂谈(1):使用公用表表达式(CTE)简化嵌套SQL》
    另外要注意的是,如果将 row_number函数用于分页处理, over子句中的 order by 与排序记录的 order by 应相同,否则生成的序号可能不是有续的。
    当然,不使用 row_number函数也可以实现查询指定范围的记录,就是比较麻烦。一般的方法是使用颠倒 Top来实现,例如,查询 t_table表中第 2条和第 3条记录,可以先查出前 3条记录,然后将查询出来的这三条记录按倒序排序,再取前 2条记录,最后再将查出来的这 2条记录再按倒序排序,就是最终结果。 SQL语句如下:

 

 

select   *   from  ( select   top   2   *   from select   top   3   *   from  t_table  order   by  field1) a  order   by  field1  desc ) b  order   by  field1

    上面的 SQL语句查询出来的结果如图 5所示。



图5
<!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->

    这个查询结果除了没有序号列 row_number,其他的与图 4所示的查询结果完全一样。

二、 rank

    rank 函数考虑到了 over子句中排序字段值相同的情况,为了更容易说明问题,在 t_table表中再加一条记录,如图 6所示。



图6
<!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->     在图6所示的记录中后三条记录的field1字段值是相同的。如果使用rank函数来生成序号,这3条记录的序号是相同的,而第4条记录会根据当前的记录数生成序号,后面的记录依此类推,也就是说,在这个例子中,第4条记录的序号是4,而不是2。rank函数的使用方法与row_number函数完全相同,SQL语句如下:

 

select  rank()  over ( order   by  field1), *   from  t_table  order   by  field1

<!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->

    上面的 SQL语句的查询结果如图 7所示。



图7
<!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->

三、 dense_rank

    dense_rank 函数的功能与 rank函数类似,只是在生成序号时是连续的,而 rank函数生成的序号有可能不连续。如上面的例子中如果使用 dense_rank函数,第 4条记录的序号应该是 2,而不是 4。如下面的 SQL语句所示:

 

 

 

select  dense_rank()  over ( order   by  field1), *   from  t_table  order   by  field1

<!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->     上面的 SQL语句的查询结果如图 8所示。



图8
<!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->

    读者可以比较图 7和图 8所示的查询结果有什么不同

 

 

四、ntile
    ntile函数可以对序号进行分组处理。这就相当于将查询出来的记录集放到指定长度的数组中,每一个数组元素存放一定数量的记录。ntile函数为每条记录生成的序号就是这条记录所有的数组元素的索引(从1开始)。也可以将每一个分配记录的数组元素称为“桶”。ntile函数有一个参数,用来指定桶数。下面的SQL语句使用ntile函数对t_table表进行了装桶处理:

 

 

select  ntile( 4 over ( order   by  field1)  as  bucket, *   from  t_table

<!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->

    上面的 SQL语句的查询结果如图 9所示。



图9

<!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->

    由于 t_table表的记录总数是 6,而上面的 SQL语句中的 ntile函数指定了桶数为 4

    也许有的读者会问这么一个问题, SQL Server2005怎么来决定某一桶应该放多少记录呢?可能 t_table表中的记录数有些少,那么我们假设 t_table表中有 59条记录,而桶数是 5,那么每一桶应放多少记录呢?

    实际上通过两个约定就可以产生一个算法来决定哪一个桶应放多少记录,这两个约定如下:

1. 编号小的桶放的记录不能小于编号大的桶。也就是说,第 1捅中的记录数只能大于等于第 2桶及以后的各桶中的记录。

2. 所有桶中的记录要么都相同,要么从某一个记录较少的桶开始后面所有捅的记录数都与该桶的记录数相同。也就是说,如果有个桶,前三桶的记录数都是 10,而第 4捅的记录数是 6,那么第 5桶和第 6桶的记录数也必须是 6

    根据上面的两个约定,可以得出如下的算法:

 

 

     //  mod表示取余,div表示取整 
     if (记录总数 mod 桶数  ==   0 )
    {
        recordCount 
=  记录总数 div 桶数;
        将每桶的记录数都设为recordCount
    } 
    
else
    {
        recordCount1 
=  记录总数 div 桶数  +   1 ;
        
int  n  =   1 ;   //   n表示桶中记录数为recordCount1的最大桶数
        m  =  recordCount1  *  n;
        
while (((记录总数  -  m)  mod  (桶数  -   n))   !=   0  )
        {
            n
++ ;
            m 
=  recordCount1  *  n;
        } 
        recordCount2 
=  (记录总数  -  m) div  (桶数  -  n);
        将前n个桶的记录数设为recordCount1
        将n 
+  1个至后面所有桶的记录数设为recordCount2
    }

<!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} -->

    根据上面的算法,如果记录总数为 59,桶数为 5,则前 4个桶的记录数都是 12,最后一个桶的记录数是 11

    如果记录总数为 53,桶数为 5,则前 3个桶的记录数为 11,后 2个桶的记录数为 10

    就拿本例来说,记录总数为 6,桶数为 4,则会算出 recordCount1的值为 2,在结束 while循环后,会算出 recordCount2的值是 1,因此,前 2个桶的记录是 2,后 2个桶的记录是 1

posted @ 2010-03-25 20:44  deepwishly  阅读(174)  评论(0编辑  收藏  举报