DISTINCT with PARTITION BY vs. GROUPBY

后端 未结 3 1609
渐次进展
渐次进展 2021-02-01 10:33

I have found some SQL queries in an application I am examining like this:

SELECT DISTINCT
Company, Warehouse, Item,
SUM(quantity) OVER (PARTITION BY Company, War         


        
3条回答
  •  一整个雨季
    2021-02-01 11:12

    Using sum() as an analytic function with over partition by is not necessary. I don't think there is a big difference between them in any sense. In oracle there are lot more analytic function than aggregation function. I think ms-sql is the same case. And for example lag(), lead(), rank(), dense rank(), etc are much harder to implement with only group by. Of course this argument is not really for defending the first version...

    Maybe there were previously more computed fields in the result set which are not implementable with group by.

提交回复
热议问题