I am trying to get the over and partition by functionality wrapped around my head. Here is an example that I just do not understand.
Here is the data I have:
<
The partition by orderdate
means that you're only comparing records to other records with the same orderdate
. For example, of the five records with orderdate = '08/01/2001'
, one will have row_number() = 1
, one will have row_number() = 2
, and so on.
The order by orderdate asc
means that, within a partition, row-numbers are to be assigned in order of orderdate
. In your example that has no effect, because you're already partitioning by orderdate
, so all records within a partition will have the same orderdate
. (It would be like writing SELECT ... FROM t WHERE c = 6 ORDER BY c
: all selected records have the same value of c
, so the ORDER BY c
does nothing.) So, within a partition, the assignment of row_number()
is arbitrary: each row will have a different number, but there are no guarantees about which row will have which number.