Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id CB54F9FB6B6 for ; Tue, 29 May 2007 09:32:53 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 22114-06 for ; Tue, 29 May 2007 09:32:50 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from an-out-0708.google.com (an-out-0708.google.com [209.85.132.242]) by postgresql.org (Postfix) with ESMTP id DCC309FB61D for ; Tue, 29 May 2007 09:32:49 -0300 (ADT) Received: by an-out-0708.google.com with SMTP id c36so492342ana for ; Tue, 29 May 2007 05:32:48 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=noZqK/lIgOaUjlhJksSsttoednfPribmB2UXPzIeFnqXsfamHPdtqcnqe2loaUALvKIORggJYdUnbABWMfthLgor4Q5H5oVT0fohsmOxSyIAva3K4LpAMWHkWHpvEP8/RhgNgnUVFyD5DX85Z/4z4m331Th+szqEqJ6SuhcV/GM= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=grzg/Jte0X883ns/JZwM5Beo45tw6hzDVAHonkB9kbc/5NhUdkKxh9CamOE093PKxEleRRZIdBv7eh/xm6duqYQWt5w+UM6fa3fCV5hhZ2e3GE5/H/hw0pGtEtRAVA2Q8khf+YgpBQFTfE6CH7fpqkDQF6fMSJb8u0NjyXz8Y38= Received: by 10.114.177.1 with SMTP id z1mr3420624wae.1180441966585; Tue, 29 May 2007 05:32:46 -0700 (PDT) Received: by 10.114.88.18 with HTTP; Tue, 29 May 2007 05:32:46 -0700 (PDT) Message-ID: Date: Tue, 29 May 2007 22:32:46 +1000 From: "Raj A" To: "Gregory Stark" Subject: Re: aggregate query Cc: "Andrew Kroeger" , pgsql-sql@postgresql.org In-Reply-To: <87odk3j43u.fsf@oxford.xeocode.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <465BDE9F.4010605@sprocks.gotdns.com> <87odk3j43u.fsf@oxford.xeocode.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200705/182 X-Sequence-Number: 28462 Thank you guys! I'm currently migrating an Oracle database to postgres and have created tables using the scripts that were readily available. Glad I can now improve this old system. On 29/05/07, Gregory Stark wrote: > "Andrew Kroeger" writes: > > > Raj A wrote: > >> I have a table > >> > >> CREATE TABLE survey_load > >> ( > >> meter_id character(5) NOT NULL, > >> number_of_bays integer NOT NULL, > >> bay_1_use integer, > >> bay_2_use integer, > >> bay_3_use integer, > >> bay_4_use integer, > >> bay_5_use integer, > >> date date NOT NULL, > >> inspection_id integer NOT NULL DEFAULT, > >> ) > >> > >> How do i present an aggregate query > >> > >> inspection_id | meter_id | bay_use > >> 1 12345 (value of bay_1_use) > >> 1 12345 (value of bay_2_use) > >> 1 12345 (value of bay_3_use) > >> 2 23456 (value of bay_1_use) > >> 2 23456 (value of bay_2_use) > >> 2 23456 (value of bay_3_use) > >> 2 23456 (value of bay_4_use) > >> 2 23456 (value of bay_5_use) > > > > > > If I understand your issue correctly, it seems like the denormalized > > nature of your table is causing you some problems. > > True. Normalizing the tables would make this query easier which is a good sign > that that's probably the right direction. > > If for some reason you can't or won't change the table definition there are a > number of possible tricky answers given the current definition. Something like > this for example: > > SELECT inspection_id, meter_id, > case when bay=1 then bay_1_use > when bay=2 then bay_2_use > when bay=3 then bay_3_use > when bay=4 then bay_4_use > when bay=5 then bay_5_use > else null > end AS bay_use > FROM ( > SELECT *, generate_series(1,number_of_bays) AS bay > FROM survey_load > ) as x > > -- > Gregory Stark > EnterpriseDB http://www.enterprisedb.com > >