X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 5C07ED1B1D2 for ; Tue, 29 Jun 2004 16:45:02 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 77015-05 for ; Tue, 29 Jun 2004 19:44:57 +0000 (GMT) Received: from wolff.to (wolff.to [66.93.249.74]) by svr1.postgresql.org (Postfix) with SMTP id D04C5D1B179 for ; Tue, 29 Jun 2004 16:44:54 -0300 (ADT) Received: (qmail 1689 invoked by uid 500); 29 Jun 2004 19:51:30 -0000 Date: Tue, 29 Jun 2004 14:51:30 -0500 From: Bruno Wolff III To: Bill Cc: pgsql-performance@postgresql.org Subject: Re: Query performance Message-ID: <20040629195130.GA1513@wolff.to> Mail-Followup-To: Bill , pgsql-performance@postgresql.org References: <40E12A5D.70007@archonet.com> <200406291733.i5THXkfL031311@math.uchicago.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200406291733.i5THXkfL031311@math.uchicago.edu> User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200406/314 X-Sequence-Number: 7372 On Tue, Jun 29, 2004 at 12:33:51 -0500, Bill wrote: > Ok, thanks. So let me explain the query number 2 as this is the more > difficult to write. So I have a list of stocks, this table contains the > price of all of the stocks at the open and close date. Ok, now we have a > ratio from query (1) that returns at least a very rough index of the daily > performance of a given stock, with each ratio representing the stock's > performance in one day. Now we need to average this with the same stock's > ratio every day, to get a total average for each stock contained in the > database. Now I would simply like to find a ratio like this that represents > the average of every stock in the table and simply find the greatest ratio. > Sorry about the lousy explanation before, is this a bit better? You can do something like: SELECT symbol, avg((open-close)/open) GROUP BY symbol ORDER BY avg((open-close)/open) DESC LIMIT 1; If you aren't interested in the variance of the daily change, it seems like you would be best off using the opening price for the first day you have recorded for the stock and the closing price on the last day and looking at the relative change.