为什么不能WHERE子句中使用ROW_NUMBER()

这篇文章给大家介绍为什么不能WHERE子句中使用ROW_NUMBER(),内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

成都创新互联成立于2013年,是专业互联网技术服务公司,拥有项目成都网站制作、做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元东区做网站,已为上家服务,为东区各地企业和个人服务,联系电话:028-86922220

主要原因: SQL的执行顺序

Here’s a common coding scenario for SQL Server developers:

“I want to see the oldest amount due for each account, along with the account number and due date, ordered by account number.”

Since the release of SQL Server 2005, the simplest way to do this has been to use a window function like ROW_NUMBER.  In many cases, everything you need can be done in a single SELECT statement with your window function. The trouble comes when you want to incorporate that function in some other way. For instance, using it a WHERE clause.

Let’s use this example from AdventureWorks2012:

为什么不能WHERE子句中使用ROW_NUMBER()

This works perfectly and gives us every row in the table. Now, let’s modify it to meet the scenario we outlined earlier, and only show the oldest order for each account.

为什么不能WHERE子句中使用ROW_NUMBER()

Because the WHERE clause already happened.

Logical Query Processing

SQL Server doesn’t process parts of a query in the same order they’re written. Rather than start with SELECT the way we read and write it, here’s the order SQL Server progresses through:

  1. FROM

  2. WHERE

  3. GROUP BY

  4. HAVING

  5. SELECT

  6. ORDER BY

  7. TOP

The first four steps are all about getting the source data and reducing the result set down. Steps 5 & 6 determine which columns are presented and in which order. Step 7 (TOP) is only applied at the end because you can’t say which rows are in the top n rows until the set has been sorted. (You can read Itzik Ben-Gan’s explanation of this process in way more detail here.)

Since the WHERE clause happens before the SELECT, it’s too late in the process to add the window function to the WHERE clause. It’d have to loop back around a second time to re-evaluate WHERE.

There’s No Magic Hack

Unfortunately, the logical query processing model is a fundamental way of doing business for SQL Server, so we can’t just cheat around it. Instead, we have to reference our window function through a subquery or CTE (common table expression). In other words, we have to code in a way that makes SQL Server evaluate a WHERE clause after the SELECT containing the window function:

为什么不能WHERE子句中使用ROW_NUMBER()

By putting the WHERE clause in the outer query and the window function in the subquery (also called an inner query), we get the window function to come before the WHERE.

为什么不能WHERE子句中使用ROW_NUMBER()

关于为什么不能WHERE子句中使用ROW_NUMBER()就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


分享文章:为什么不能WHERE子句中使用ROW_NUMBER()
网页网址:http://bzwzjz.com/article/isjchp.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 成都网站制作 网站建设方案 营销网站建设 成都网站设计 攀枝花网站设计 外贸营销网站建设 成都网站设计制作公司 手机网站制作设计 成都网站制作 响应式网站设计 专业网站设计 定制网站设计 成都网站建设 专业网站建设 网站设计 定制网站建设 成都网站建设 成都商城网站制作 成都网站建设公司 成都网站建设 成都网站建设 网站制作公司