SQL Server: Difference between PARTITION BY and GROUP BY

Posted Chuck Lu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL Server: Difference between PARTITION BY and GROUP BY相关的知识,希望对你有一定的参考价值。

https://stackoverflow.com/questions/2404565/sql-server-difference-between-partition-by-and-group-by

 

They‘re used in different places. group by modifies the entire query, like:

select customerId, count(*) as orderCount
from Orders
group by customerId

But partition by just works on a window function, like row_number:

select row_number() over (partition by customerId order by orderId)
    as OrderNumberForThisCustomer
from Orders

A group by normally reduces the number of rows returned by rolling them up and calculating averages or sums for each row. partition by does not affect the number of rows returned, but it changes how a window function‘s result is calculated.

以上是关于SQL Server: Difference between PARTITION BY and GROUP BY的主要内容,如果未能解决你的问题,请参考以下文章

SQL SERVER 2008 R2数据库出现“远程过程调用失败”(0x800706be)错误,怎么办!!

set_difference的介绍

SQL Server-Resource Monitor worker appears to be non-yielding on Node 0

SQL Server 2008 累计运行总计

SQL_Server_2005_函数大全(描述及实例)

The difference between UDS on IP and UDS on CAN