X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id CE21BD711F for ; Tue, 11 Oct 2005 11:25:38 -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 07903-10 for ; Tue, 11 Oct 2005 14:25:35 +0000 (GMT) Received: from mail.metronet.co.uk (mail.metronet.co.uk [213.162.97.75]) by svr1.postgresql.org (Postfix) with ESMTP id 3DAA9D70A2 for ; Tue, 11 Oct 2005 11:25:37 -0300 (ADT) Received: from mainbox.archonet.com (84-51-143-99.archon037.adsl.metronet.co.uk [84.51.143.99]) by smtp.metronet.co.uk (MetroNet Mail) with ESMTP id 1020940DCAF; Tue, 11 Oct 2005 15:25:31 +0100 (BST) Received: from localhost (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with ESMTP id 4751315ED9; Tue, 11 Oct 2005 15:24:36 +0100 (BST) Received: from mainbox.archonet.com ([127.0.0.1]) by localhost (mainbox [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 19923-04; Tue, 11 Oct 2005 15:24:31 +0100 (BST) Received: from [192.168.1.17] (client17.office.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id B192515ED5; Tue, 11 Oct 2005 15:24:31 +0100 (BST) Message-ID: <434BCB1F.7040403@archonet.com> Date: Tue, 11 Oct 2005 15:24:31 +0100 From: Richard Huxton User-Agent: Thunderbird 1.4.1 (X11/20051006) MIME-Version: 1.0 To: Neil Saunders Cc: pgsql-sql@postgresql.org Subject: Re: Difference from average References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.059 required=5 tests=[AWL=0.009, FORGED_RCVD_HELO=0.05] X-Spam-Level: X-Archive-Number: 200510/89 X-Sequence-Number: 22942 Neil Saunders wrote: > Hi all, > > I'm developing a property rental database. One of the tables tracks > the price per week for different properties: > > CREATE TABLE "public"."prices" ( > "id" SERIAL, > "property_id" INTEGER, > "start_date" TIMESTAMP WITHOUT TIME ZONE, > "end_date" TIMESTAMP WITHOUT TIME ZONE, > "price" DOUBLE PRECISION NOT NULL > ) WITH OIDS; > > CREATE INDEX "prices_idx" ON "public"."prices" > USING btree ("property_id"); > > I'd like to display the prices per property in a table, with each row > coloured different shades; darker shades representing the more > expensive periods for that property. To do this, I propose to > calculate the percentage difference of each rows price from the > average for that property, so if for example I have two rows, one for > price=200 and one for price=300, i'd like to retrieve both records > along with the calculated field indicating that the rows are -20%, > +20% from the average, respectively. > > I've started with the following query, but since I'm still learning > how PostgreSQL works, I'm confused as to the efficiency of the > following statement: > > SELECT *, (price - (SELECT avg(price) from prices)) as diff FROM prices; I'd personally write it something like: SELECT prices.property_id, prices.price AS actual_price, averages.avg_price, (averages.avg_price - prices.price) AS price_diff ((averages.avg_price - prices.price)/averages.avg_price) AS pc_diff FROM prices, (SELECT property_id, avg(price) as avg_price FROM prices) AS averages WHERE prices.property_id = averages.property_id ; That's as much to do with how I think about the problem as to any testing though. -- Richard Huxton Archonet Ltd