Skip to content
Advertisement

Nested SUM with OVER clause

I am trying to understand how nested SUMs with OVER clause work in SQL. Let’s say i have the following schema in my DB

and suppose that i want to calculate the (number of deliveries of each region)/(number of deliveries of the state) on the same year. The query to do the calculation is:

I can’t understand how the PARTITION BY works in this case. Particularly i can’t understand which data are passed to the analytic function (OVER clause).

Does the PARTITION BY works on the whole dataset or on the rows aggregated by the GROUP BY?

Thanks a lot to everyone.

Advertisement

Answer

Window functions are set based, and generally evaluate last in the query, after everything else has evaluated. Here is your call to SUM() again:

This will evaluate after the GROUP BY, at which point the intermediate table will have one record for each region, state, and year. Your analytic function call sums the sum over a window of all regions in each state and year. So your expression can be summarized as:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement