Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ozKvz-00051t-Fl for pgsql-sql@arkaria.postgresql.org; Sun, 27 Nov 2022 16:55:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ozKvy-0000IT-8S for pgsql-sql@arkaria.postgresql.org; Sun, 27 Nov 2022 16:55:14 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ozKvx-0000I9-UU for pgsql-sql@lists.postgresql.org; Sun, 27 Nov 2022 16:55:13 +0000 Received: from mail-pj1-x1032.google.com ([2607:f8b0:4864:20::1032]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ozKvv-0002Vq-ER for pgsql-sql@lists.postgresql.org; Sun, 27 Nov 2022 16:55:13 +0000 Received: by mail-pj1-x1032.google.com with SMTP id w4-20020a17090ac98400b002186f5d7a4cso11858381pjt.0 for ; Sun, 27 Nov 2022 08:55:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:from:references:cc:to:content-language:subject :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=ZUsul3E6ojNd9xpGKOT3X0Yb/tQ2RqnwOPJTUPiarXs=; b=U50kAsKdAIZsaiKeonBcVxXyXkCcssfR+KWzTMNjge8lUf/xBcuz9eketjm2kParqz Lu7kq8tsAnCdF0CNjh3d0DVSX+1pQx1ZGtrTfd9JfOO8zNzC6+loqcdu+ZrtYZSJgGcz Rznewl5SyQXhkkNoJ5My9uBzZsZv6q8O55MGNUHvsDINYznXmweBowaYf7+69oVaqXeh ozsNUpaBbiZfGegpWofXnOPB66N9qvO9mgFbt9Ybd5G6Nd0s77kdo8Sq+Cno66xd7YYQ 9cIrDRH1oSkv9jVCP5mMyzvynBBI0g5+Odi8nLilpIkq4iYpq4jEDXghC/bWC2Kd+E9R w+ag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:from:references:cc:to:content-language:subject :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=ZUsul3E6ojNd9xpGKOT3X0Yb/tQ2RqnwOPJTUPiarXs=; b=Uav/0zuvzy3ougIIgXQFf40awtUP6kecX/+wV8zgBAUZUDXR1MZY87czRgGCxSV3e4 mZPjcfr1IzOjj4wckLIRdAp9wPWQruOqSKrRkGSIMtvpMrw/cY1kJoVujbQRKM+qtrDH wJhdjyk6latYqEVLOwtPLz0nlPLKoD7hXwJvB5ePqbKASaprJ27/UU8Fe6XA75OmULTd jtgZHh6QXTktLqgfV+etAKiaAryYk3zOB6ZlF6yeQeP89KUxo14l1waoBuC67lDnKwm/ DK7D4vSNFjwKfejgx0xY1VZPUmSu+L9ys1w1AInIU6SP90h5WYAgt3tmgFppDm+CVEA1 0wFg== X-Gm-Message-State: ANoB5pnppzoDWwAT9HTUJHc5/qA1GE3ktUC/utaHdXI/o6IxJoNvfBTA TrFH+6nELwkMi+wfAdU5OpRJPok+Q94= X-Google-Smtp-Source: AA0mqf4hpwt1ZWaFkm7Rd83lTLQ68H8uWN+JNJsY0l5QCfjDP6cMq+XeoLg/Gv0Is/rUdFT8eekT6w== X-Received: by 2002:a17:90a:f683:b0:219:2e19:14ca with SMTP id cl3-20020a17090af68300b002192e1914camr3176935pjb.204.1669568109074; Sun, 27 Nov 2022 08:55:09 -0800 (PST) Received: from [10.0.2.15] ([122.163.232.215]) by smtp.gmail.com with ESMTPSA id n31-20020a17090a5aa200b002187a4dd830sm8072332pji.46.2022.11.27.08.55.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 27 Nov 2022 08:55:08 -0800 (PST) Content-Type: multipart/alternative; boundary="------------Mi6uyJslqv7iYzXOs2funsQf" Message-ID: <2e8fb31d-b09f-f195-b810-6e6683c656cc@gmail.com> Date: Sun, 27 Nov 2022 22:25:05 +0530 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Subject: Re: [QUESTION] Window function with partition by and order by Content-Language: en-US To: Samed YILDIRIM Cc: pgsql-sql@lists.postgresql.org References: <7f9f9b1d-b612-d0bd-4674-cfb5a8b2d343@gmail.com> From: Ankit Kumar Pandey In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------Mi6uyJslqv7iYzXOs2funsQf Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 27/11/22 21:53, Samed YILDIRIM wrote: > Hello Ankit, > > It is absolutely expected behaviour of a window function with ORDER BY > clause. The default frame clause of window definition is *RANGE > BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW*. If you add an ORDER BY > clause in a window definition, PostgreSQL takes the current row and > all rows before it within the partition into calculation. If you don't > add, it means all rows within the partition are peers, and PostgreSQL > uses all rows for calculation. I'm putting the related part from the > documentation and its link below. > > The default framing option is RANGE UNBOUNDED PRECEDING, which is > the same as RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW; it > sets the frame to be all rows from the partition start up through > the current row's last peer (a row that the window's ORDER BY > clause considers equivalent to the current row; all rows are peers > if there is no ORDER BY). > > https://www.postgresql.org/docs/15/sql-select.html#SQL-WINDOW > > Best regards. > Samed YILDIRIM > > > On Sun, 27 Nov 2022 at 18:08, Ankit Kumar Pandey > 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 > > Okay, I understand this now. -- Regards, Ankit Kumar Pandey --------------Mi6uyJslqv7iYzXOs2funsQf Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit


On 27/11/22 21:53, Samed YILDIRIM wrote:
Hello Ankit,

It is absolutely expected behaviour of a window function with ORDER BY clause. The default frame clause of window definition is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. If you add an ORDER BY clause in a window definition, PostgreSQL takes the current row and all rows before it within the partition into calculation. If you don't add, it means all rows within the partition are peers, and PostgreSQL uses all rows for calculation. I'm putting the related part from the documentation and its link below.

The default framing option is RANGE UNBOUNDED PRECEDING, which is the same as RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW; it sets the frame to be all rows from the partition start up through the current row's last peer (a row that the window's ORDER BY clause considers equivalent to the current row; all rows are peers if there is no ORDER BY).
 

Best regards.
Samed YILDIRIM


On Sun, 27 Nov 2022 at 18:08, Ankit Kumar Pandey <itsankitkp@gmail.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


Okay, I understand this now.

-- 
Regards,
Ankit Kumar Pandey
--------------Mi6uyJslqv7iYzXOs2funsQf--