X-Original-To: pgsql-general-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id DE2F6D1B51F; Mon, 29 Sep 2003 19:40:46 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 56913-05; Mon, 29 Sep 2003 16:40:16 -0300 (ADT) Received: from mail1.ihs.com (mail1.ihs.com [170.207.70.222]) by svr1.postgresql.org (Postfix) with ESMTP id D40A3D1B4F0; Mon, 29 Sep 2003 16:40:14 -0300 (ADT) Received: from css120.ihs.com (css120.ihs.com [170.207.105.120]) by mail1.ihs.com (8.12.10/8.12.10) with ESMTP id h8TJcsvA017118; Mon, 29 Sep 2003 13:38:54 -0600 (MDT) Date: Mon, 29 Sep 2003 13:33:24 -0600 (MDT) From: "scott.marlowe" To: Rasmus Aveskogh Cc: , Subject: Re: [SQL] Result set granularity.. In-Reply-To: <59410.193.243.134.6.1064668289.squirrel@www.defero.se> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-IHS-MailScanner: Found to be clean X-IHS-MailScanner-SpamCheck: X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200309/1820 X-Sequence-Number: 50039 On Sat, 27 Sep 2003, Rasmus Aveskogh wrote: > > Hi, > > Since I went from Oracle to PostgreSQL I've been missing the "invisable" > column 'rownum'. I often used it to lower the granularity of my data. > For example, say I have a large table containing some sort of statistical > data and want to plot a graph using it. > If the graph is 600 pixels wide I might as well lower the granularity of > my incoming data to 600 measure points before plotting. > > In Oracle I used to do this by using the modulus operator on the rownum > column as a restriction. > > SELECT FROM WHERE mod(rownum, 5) = 0; > > The query above would give me every fifth row of the original result set > and would save me from sending the data over my database connection and do > the lowering of the granularity in the application. > > I have two questions, one dependent on the answer on the other one.. > > 1) Is it possible to achieve this any other _easy_ way? > Perhaps it would be feasible to write a wrapper using a counter which > makes the SELECT and then return every fifth row to the SELECT calling the > wrapper. But then I assume the data still has to "travel" one step which > puts on some overhead. The standard "trick" in Postgresql is to create a temporary sequence and select that as well: create temp sequence aaa; postgres=# select *, nextval('aaa') from accounts; to get a row number. The sequence creation and use is pretty fast, and I've used it before. > > 2) Would it be possible to add one more limit argument to the non-standard > set of LIMIT and OFFET - a 'SCALE n' or 'GRANUL n' argument which would > return every n row of the initial result set. I think that would be > gladly accepted for folks working with statistical data. Using the same trick, you can get every 5th row like this: select * from (select *, nextval('aaa') as row from accounts) as a where a.row%5=4;