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 9E252D1B182 for ; Tue, 29 Jun 2004 16:03:37 -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 55866-03 for ; Tue, 29 Jun 2004 19:03:30 +0000 (GMT) Received: from anchor-post-37.mail.demon.net (anchor-post-36.mail.demon.net [194.217.242.86]) by svr1.postgresql.org (Postfix) with ESMTP id 643CDD1B258 for ; Tue, 29 Jun 2004 16:03:27 -0300 (ADT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-37.mail.demon.net with esmtp (Exim 3.35 #1) id 1BfNt9-0007ZU-0b; Tue, 29 Jun 2004 20:03:27 +0100 Received: from [192.168.1.17] (client17.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id C4ABB16520; Tue, 29 Jun 2004 20:03:26 +0100 (BST) Message-ID: <40E1BCFD.9090401@archonet.com> Date: Tue, 29 Jun 2004 20:03:25 +0100 From: Richard Huxton User-Agent: Mozilla Thunderbird 0.7 (X11/20040615) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Bill Cc: pgsql-performance@postgresql.org Subject: Re: Query performance References: <200406291733.i5THXkfL031311@math.uchicago.edu> In-Reply-To: <200406291733.i5THXkfL031311@math.uchicago.edu> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit 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/312 X-Sequence-Number: 7370 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? > > Here is an example if needed. > > Say we have a stock by the name of YYY > > I know, due to query 1 that stock YYY has a abs(close-open)/open price ratio > of for example, 1.3 on Dec 1 and (for simplicity let's say we only have two > dates) and Dec 2 the ratio for YYY is 1.5. So the query averages and gets > 1.4. Now it needs to do this for all of the stocks in the table and sort by > increasing ratio. Well, the simplest would be something like: CREATE VIEW my_ratios AS SELECT ...(select details we used for #1 previously) Query #1 then becomes: SELECT * FROM my_ratios; Then you could do: SELECT symbol, avg(ratio) as ratio_avg FROM my_ratios GROUP BY symbol ORDER BY avg(ratio) ; Now, in practice, I'd probably create a symbol_ratio table and fill that one day at a time. Then #2,#3 would be easier. -- Richard Huxton Archonet Ltd