Re: [QUESTION] Window function with partition by and order by

From: William Alves Da Silva <william_silva(at)unochapeco(dot)edu(dot)br>
To: pgsql-sql(at)lists(dot)postgresql(dot)org, Ankit Kumar Pandey <itsankitkp(at)gmail(dot)com>
Subject: Re: [QUESTION] Window function with partition by and order by
Date: 2022-11-27 16:21:44
Message-ID: 2cc5a0eb-ef51-4561-9bc5-d015aab92228@Spark
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hello Ankit.

This behavior is correct. This is because you are using ORDER BY in your aggregation function.

Looking at the documentation you will find the following quote:
"You can also control the order in which rows are processed by window functions by using ORDER BY within OVER. (The window ORDER BY doesn't even have to match the order in which the rows are produced)."

So, if you use ORDER BY you are controlling how the row are processed.

If you don't use, the result is like this:

postgres=# select *, avg(id) over (partition by name) from my_teste ;
 id | name | avg
----+------+--------------------
 1 | A | 1.5000000000000000
 1 | A | 1.5000000000000000
 3 | A | 1.5000000000000000
 1 | A | 1.5000000000000000
 2 | B | 2.0000000000000000
(5 rows)

--
Regards,
William Alves
On 27 Nov 2022 13:08 -0300, Ankit Kumar Pandey <itsankitkp(at)gmail(dot)com>, wrote:
> Hello,
>
> While looking at aggregates in window function, I found something
> unusual and would be glad I could get some clarification.
>
> Consider following table (mytable):
>
> id, name
>
> 1, A
>
> 1, A
>
> 2, B
>
> 3, A
>
> 1, A
>
>
> select *, avg(id) over (partition by name, order by id) from mytable;
>
> Output:
>
> id, name, avg
>
> 1, A, 1
>
> 1, A, 1
>
> 1, A, 1
>
> 3, A, 1.5
>
> 2, B, 2
>
>
> Question is: Average of id for partition name (A) should be 6/4 = 1.5
> for all rows in that partition but this result is seen only at the last
> one row in partition (A). Am I missing here something?
>
>
> Thanks
>
>
> --
> Regards,
> Ankit Kumar Pandey
>
>
>

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Samed YILDIRIM 2022-11-27 16:23:38 Re: [QUESTION] Window function with partition by and order by
Previous Message Ankit Kumar Pandey 2022-11-27 16:08:33 [QUESTION] Window function with partition by and order by