Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id BC7828799B8 for ; Thu, 16 Feb 2012 21:33:28 -0400 (AST) Received: from nm3.bullet.mail.ne1.yahoo.com ([98.138.90.66]) by makus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1RyChT-0004yv-PP for pgsql-sql@postgresql.org; Fri, 17 Feb 2012 01:33:28 +0000 Received: from [98.138.90.52] by nm3.bullet.mail.ne1.yahoo.com with NNFMP; 17 Feb 2012 01:33:15 -0000 Received: from [98.138.89.232] by tm5.bullet.mail.ne1.yahoo.com with NNFMP; 17 Feb 2012 01:33:15 -0000 Received: from [127.0.0.1] by omp1047.mail.ne1.yahoo.com with NNFMP; 17 Feb 2012 01:33:15 -0000 X-Yahoo-Newman-Id: 231816.73589.bm@omp1047.mail.ne1.yahoo.com Received: (qmail 61905 invoked from network); 17 Feb 2012 01:33:15 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=DKIM-Signature:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:References:In-Reply-To:Mime-Version:Content-Transfer-Encoding:Content-Type:Message-Id:Cc:X-Mailer:From:Subject:Date:To; b=FlupldKSh9LZSXKm8GP7GjCd8FFm9CweSyzFgwzIHX5MRRZi2Osx/p3OyvUGFx6iA5fMV0891EI6lAPgqOXPwAQmeoOOUvPnDO47CkoyTUDY/ihtBoAYfln3VyKMfa9lhnNCqrZkKtsAeR8nxB2OJyt3nYvSoUyKKPY3UtMFPHM= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1329442395; bh=RgHlVpGeMZE5twQM28mqnWlIv5lcG/lcRuftBP/vj9o=; h=X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:References:In-Reply-To:Mime-Version:Content-Transfer-Encoding:Content-Type:Message-Id:Cc:X-Mailer:From:Subject:Date:To; b=piRdnJ7Qk/HBH/6cxJj0MUkbBvkeQ1FA7tZ5sTNdcMVvMWBH+9ORwp8tCznh4Alm1r1AmW/ubsncbxMq1O4W0eqHqj5duE1Uc0yCb6Wi3GU+SXIsv7DecD3gAvwzN2gLN8jxn5Evb6Jkk8D+E/s1yEqdAEgwSE837kZdknyJ/50= X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: CR7pXSIVM1n47Ed8Vqn.a1CC6bRFaMOxwQPFsPknqEITM2f vf81qsPpA.MJnvf2Cj6w6BJnsxLXR2c90Fwkd7hdzlMTF_vnvAHpYJhSIJ04 spmKIyVqXGShKeAIWfZugN8PtLzjIBgege7qlAXETwY4y4ccoMw4rk__4RBg CbuTT1l4D9zEBUx9Zb6T5p9sfuM0nib06485k77nQ2oDIJA8Pknhk9z3yDBH gq9eBMi3ySrbqhhWOLv3ISivf4XK9Ctsj0RRvgWvjhVQshWSoz3Y_LBhhR8d lj0zEbgR15GefnHHJyLt.bxemOXBjcoCZKZjgk1QBwZgDzGonXK.Xvv.6.sy FHxflthltf.CU.86jh9cXqgXRsonw_QYYj78gQ8HTXVLLW_rRPvlbtoVSFE3 ogDLeDmwMnCvAC2hjr3TJ X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from [192.12.17.102] (polobo@24.93.23.188 with xymcookie) by smtp118-mob.biz.mail.ne1.yahoo.com with SMTP; 16 Feb 2012 17:33:14 -0800 PST References: <1329440503038-5491171.post@n5.nabble.com> In-Reply-To: <1329440503038-5491171.post@n5.nabble.com> Mime-Version: 1.0 (1.0) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Message-Id: <4DA2930E-CB3F-4925-8DD2-7D541E5087F0@yahoo.com> Cc: "pgsql-sql@postgresql.org" X-Mailer: iPad Mail (9A405) From: David Johnston Subject: Re: Window function frame clause Date: Thu, 16 Feb 2012 20:33:16 -0500 To: vpapavas X-Pg-Spam-Score: -0.5 (/) X-Archive-Number: 201202/56 X-Sequence-Number: 36349 On Feb 16, 2012, at 20:01, vpapavas wrote: > Hello all,=20 >=20 > I am trying to use this query in a toy database with customers and orders i= n > order to understand the capabilities of partitioning. In plain english wha= t > I want to do is to select the orders of each customer and return only 3 of= > those orders.=20 >=20 > The query I am using is this:=20 > select c_custkey, o_orderkey, o_orderpriority, id from ( > select c_custkey, o_orderkey, o_orderpriority, o_totalprice, row_number()=20= > over(PARTITION BY c_custkey ROWS between UNBOUNDED PRECEDING and 3 > FOLLOWING) as id=20 > from customers left outer join orders on c_custkey =3D o_custkey) as temp >=20 > Although I am using the frame clause ROWS between UNBOUNDED PRECEDING and 3= > FOLLOWING which in my understanding should return the first row of the > partition and the three following, this query returns all rows in the > partition. Am I doing something wrong? Or have I understood wrong the > semantics of the frame clause? I am using Postgresql v9.1 >=20 > I rewrote the query like this in order to make it work: > select c_custkey, o_orderkey, o_orderpriority, id from ( > select c_custkey, o_orderkey, o_orderpriority, row_number() > over(PARTITION BY c_custkey) as id=20 > from customers left outer join orders on c_custkey =3D o_custkey ) as temp= > where id <=3D 3 >=20 > but the problem is that I would like to not have to compute the entire joi= n > since I am interested in only 3 orders for each customer.=20 >=20 > Thank you, > Vicky >=20 >=20 Put the window function on the order table, perform the where-limit, then jo= in customer to the result. Also, you are numbering rows but not imposing any kind of order before doing= so. Row_number doesn't make sense with a frame clause...frame is more useful for= stuff like calculating rolling sums/averages and the like - where you evalu= ate fields in the surrounding frame as part of the aggregate. Window functions do not affect the number of rows returned. David J.