Home  >  Article  >  Database  >  SqlServer 2005 T-SQL Query 学习笔记(2)

SqlServer 2005 T-SQL Query 学习笔记(2)

WBOY
WBOYOriginal
2016-06-07 18:05:12789browse

SqlServer 2005 T-SQL Query 学习笔记(2)

SQL2005增加了4个关于队计算的函数:分别是ROW_NUMBER,RANK,DENSE_RANK,NTILE.

注意:这些函数只能出现在SELECT和ORDER BY的查询中。语法如下:

ranking_function over([partition by col_list] order by col_list)

ROW_NUMBER:在排序的基础上对所有列进行连续的数字进行标识。

执行顺序:为了计算列值,优化器首先需要把数据在分区列上进行排序,然后在对这些列进行编码。

SQL2005之前的技术处理列计算

1.(SET-BASED)

在SQL2005之前,已经有了简单的对列集合的计算,使用unique partitioning + sort 组合。

比如,你可以使用下面的技术:

empiddboSales S2
  S2empid S1empidrownum
dboSales S1
empid

这是非常简单的,但也是非常慢的。。。

如果需要组合条件产生列数(即非唯一列的组合排序和断路器),可以这样做:

empidqtydboSales S2
  S2qty S1qty
   S2qty S1qty S2empid S1empidrownum
dboSales S1
qtyempid

当然还有很多方法,比如用游标,就不写例子了。

2.(IDENTITY-Based Solution)

empidqty11rn
#SalesRN dboSales#SalesRN#SalesRN
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn