Re: master-detail relationship and count

From: Richard Huxton <dev(at)archonet(dot)com>
To: Gary Stainburn <gary(dot)stainburn(at)ringways(dot)co(dot)uk>, pgsql-sql(at)postgresql(dot)org
Subject: Re: master-detail relationship and count
Date: 2002-11-29 10:35:12
Message-ID: 200211291035.12923.dev@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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 =
> 'R' and l.lklid = r.rtid;

You'll want to GROUP BY

SELECT r.rtid, r.rtname, l.count(*) FROM route r, links l WHERE l.lktype='R'
AND l.lklid=r.rtid GROUP BY r.rtid, r.rtname;

--
Richard Huxton

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Achilleus Mantzios 2002-11-29 10:36:05 Re: master-detail relationship and count
Previous Message Richard Huxton 2002-11-29 10:32:26 Re: Analyze + Index