Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id BAAEE6325D4 for ; Fri, 26 Mar 2010 03:45:44 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 59414-08 for ; Fri, 26 Mar 2010 06:45:30 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.17.9]) by mail.postgresql.org (Postfix) with ESMTP id C640A6323B7 for ; Fri, 26 Mar 2010 03:45:32 -0300 (ADT) Received: from webserv.wug.de ([194.25.154.138]) by mrelayeu.kundenserver.de (node=mrbap0) with ESMTP (Nemesis) id 0Lv8fS-1NVJHc2OZO-00ziNK; Fri, 26 Mar 2010 07:45:31 +0100 Received: from kretschmer by webserv.wug.de with local (Exim 3.36 #1) id 1Nv3IP-0002NN-00 for pgsql-sql@postgresql.org; Fri, 26 Mar 2010 07:45:29 +0100 Date: Fri, 26 Mar 2010 07:45:29 +0100 From: "A. Kretschmer" To: pgsql-sql@postgresql.org Subject: Re: Help me with this multi-table query Message-ID: <20100326064528.GB6817@a-kretschmer.de> References: <4BA8D216.3000802@itech7.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4BA8D216.3000802@itech7.com> X-OS: Debian/GNU Linux - weil ich es mir Wert bin! User-Agent: Mutt/1.5.9i X-Provags-ID: V01U2FsdGVkX1/WH2mUDCWw2JvIluqbm4p4cPJ0zpqpUEPcyb3 aHZQtMInoePutYNTvTwH7sf2ntrBLQAULGtvzjSo/e62ZWSlEg Fw6y4+OPFtOthwkAlQUFA== X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.972 tagged_above=-10 required=5 tests=AWL=0.627, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201003/200 X-Sequence-Number: 33907 In response to Nilesh Govindarajan : > Hi, > > I want to find out the userid, nodecount and comment count of the userid. > > I'm going wrong somewhere. > > Check my SQL Code- Check my example: test=*# select * from u; id ---- 1 2 3 (3 rows) test=*# select * from n; uid ----- 1 1 1 2 2 3 (6 rows) test=*# select * from c; uid ----- 1 2 2 3 3 3 (6 rows) test=*# select foo.id, foo.n_count, count(c) from (select u.id, count(n) as n_count from u right join n on (n.uid=u.id) group by 1) foo left join c on foo.id=c.uid group by id, n_count; id | n_count | count ----+---------+------- 1 | 3 | 1 2 | 2 | 2 3 | 1 | 3 (3 rows) Andreas -- Andreas Kretschmer Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header) GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431 2EB0 389D 1DC2 3172 0C99