Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 9D780476033 for ; Fri, 29 Nov 2002 05:45:04 -0500 (EST) Received: from anchor-post-32.mail.demon.net (anchor-post-32.mail.demon.net [194.217.242.90]) by postgresql.org (Postfix) with ESMTP id 0D6D8475AEC for ; Fri, 29 Nov 2002 05:45:01 -0500 (EST) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-32.mail.demon.net with esmtp (Exim 3.35 #1) id 18Hidq-000Bqt-0W; Fri, 29 Nov 2002 10:45:02 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with ESMTP id 17E7E173F2; Fri, 29 Nov 2002 10:35:13 +0000 (GMT) Received: from client.archonet.com (client.archonet.com [192.168.1.16]) by mainbox.archonet.com (Postfix) with ESMTP id 885C8172A0; Fri, 29 Nov 2002 10:35:12 +0000 (GMT) Content-Type: text/plain; charset="iso-8859-1" From: Richard Huxton Organization: Archonet Ltd To: Gary Stainburn , pgsql-sql@postgresql.org Subject: Re: master-detail relationship and count Date: Fri, 29 Nov 2002 10:35:12 +0000 User-Agent: KMail/1.4.3 References: <200211291006.06749.gary.stainburn@ringways.co.uk> In-Reply-To: <200211291006.06749.gary.stainburn@ringways.co.uk> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-Id: <200211291035.12923.dev@archonet.com> X-Virus-Scanned: by AMaViS snapshot-20020531 X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200211/435 X-Sequence-Number: 10577 On Friday 29 Nov 2002 10:06 am, Gary Stainburn wrote: > Hi folks. > > I've got a master detail relationship where I have a railway route table > listing landmarks along the route, and a Links table listing URL's > associated with that landmark. Listed below: > > How can I do a query showing the landmark ID, the landmark name, and a > count of links associated with that landmark. Below is a SQL statement > that although is illegal, gives a good idea of what I'm looking for. > > select r.rtid, r.rtname, l.count(*) from route r, links l where l.lktype = =3D > 'R' and l.lklid =3D r.rtid; You'll want to GROUP BY SELECT r.rtid, r.rtname, l.count(*) FROM route r, links l WHERE l.lktype=3D= 'R'=20 AND l.lklid=3Dr.rtid GROUP BY r.rtid, r.rtname; --=20 Richard Huxton