X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 688C311F6B5A for ; Fri, 21 Apr 2006 09:27:39 -0300 (ADT) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Score: 0.106 X-Spam-Level: X-Spam-Status: No, score=0.106 required=5 tests=[AWL=0.106] Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id boJXnThjlGKU for ; Fri, 21 Apr 2006 09:27:33 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from amanda.contactbda.com (ipn36372-e65122.cidr.lightship.net [216.204.66.226]) by postgresql.org (Postfix) with ESMTP id 725B111F76F4 for ; Fri, 21 Apr 2006 09:27:36 -0300 (ADT) Received: from amanda.contactbda.com (amanda.contactbda.com [192.168.1.2]) by amanda.contactbda.com (8.12.11/8.12.11/Debian-3) with ESMTP id k3LCRWru007655; Fri, 21 Apr 2006 08:27:32 -0400 From: "Jim Buttafuoco" To: Jan Dittmer , pgsql-performance@postgresql.org Reply-To: jim@contactbda.com Subject: Re: Better way to write aggregates? Date: Fri, 21 Apr 2006 08:27:32 -0400 Message-Id: <20060421122617.M81850@contactbda.com> In-Reply-To: <444899B6.6090001@l4x.org> References: <444899B6.6090001@l4x.org> X-Mailer: Open WebMail 2.41 20040926 X-OriginatingIP: 192.168.1.1 (jim) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 X-Archive-Number: 200604/497 X-Sequence-Number: 18581 Jan, I write queries like this CREATE VIEW parent_childs AS SELECT c.parent, count(c.state) as childtotal, sum(case when c.state = 1 then 1 else 0 end) as childstate1, sum(case when c.state = 2 then 1 else 0 end) as childstate2, sum(case when c.state = 3 then 1 else 0 end) as childstate3 FROM child c GROUP BY parent; ---------- Original Message ----------- From: Jan Dittmer To: pgsql-performance@postgresql.org Sent: Fri, 21 Apr 2006 10:37:10 +0200 Subject: [PERFORM] Better way to write aggregates? > Hi, > > I more or less often come about the problem of aggregating a > child table counting it's different states. The cleanest solution > I've come up with so far is: > > BEGIN; > CREATE TABLE parent ( > id int not null, > name text not null, > UNIQUE(id) > ); > > CREATE TABLE child ( > name text not null, > state int not null, > parent int not null references parent(id) > ); > > CREATE VIEW parent_childs AS > SELECT > c.parent, > count(c.state) as childtotal, > count(c.state) - count(nullif(c.state,1)) as childstate1, > count(c.state) - count(nullif(c.state,2)) as childstate2, > count(c.state) - count(nullif(c.state,3)) as childstate3 > FROM child c > GROUP BY parent; > > CREATE VIEW parent_view AS > SELECT p.*, > pc.* > FROM parent p > LEFT JOIN parent_childs pc ON (p.id = pc.parent); > COMMIT; > > Is this the fastest way to build these aggregates (not considering > tricks with triggers, etc)? The count(state) - count(nullif(...)) looks > a bit clumsy. > I also experimented with a pgsql function to sum these up, but considered > it as not-so-nice and it also always forces a sequential scan on the > data. > > Thanks for any advice, > > Jan > > ---------------------------(end of broadcast)--------------------------- > TIP 6: explain analyze is your friend ------- End of Original Message -------