首页 > 数据库技术 > 详细

SQL Server: Difference between PARTITION BY and GROUP BY

时间:2018-06-11 12:43:18      阅读:165      评论:0      收藏:0      [点我收藏+]

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

原文:https://www.cnblogs.com/chucklu/p/9166320.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!