Re: master-detail relationship and count - Mailing list pgsql-sql

From Richard Huxton
Subject Re: master-detail relationship and count
Date
Msg-id 200211291035.12923.dev@archonet.com
Whole thread Raw
In response to master-detail relationship and count  (Gary Stainburn <gary.stainburn@ringways.co.uk>)
List 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


pgsql-sql by date:

Previous
From: Richard Huxton
Date:
Subject: Re: Analyze + Index
Next
From: Gary Stainburn
Date:
Subject: Re: master-detail relationship and count