Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Windowing functions: does the order of appearance for columns matter in partition by?

I'm trying to better understand the analytical functions in SQL.

  1. Is my understanding correct that the windowing function will be applied to every unique combination of fields that appear in "partition by"?

  2. If I were to partition my result-set by several fields, does the order of appearance of these fields matter?

To be more specific, would the results of the following two queries differ in any circumstances?

Select customer_code,
       state,
       weekOfDate, 
       SUM(Sales) over(partition by customer_code,state,weekOfDate)
From Sales
Select customer_code, state, weekOfDate, SUM(Sales) over(partition by weekOfDate,state,customer_code) From Sales
like image 810
x.farhad Avatar asked Jan 29 '26 11:01

x.farhad


1 Answers

#1: yes

#2: no

PARTITION BY is similar to GROUP BY, ORDER doesn't matter

like image 95
dnoeth Avatar answered Jan 31 '26 03:01

dnoeth