pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Huxton <dev@archonet.com>
To: Gary Stainburn <gary.stainburn@ringways.co.uk>
To: pgsql-sql@postgresql.org
Subject: Re: master-detail relationship and count
Date: Fri, 29 Nov 2002 10:35:12 +0000
Message-ID: <200211291035.12923.dev@archonet.com> (raw)
In-Reply-To: <200211291006.06749.gary.stainburn@ringways.co.uk>
References: <200211291006.06749.gary.stainburn@ringways.co.uk>

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



view thread (8+ messages)  latest in thread

Message-ID: <200211291035.12923.dev@archonet.com>
Permalink:  ../200211291035.12923.dev@archonet.com/
Also on:    postgresql.org/message-id/200211291035.12923.dev@archonet.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: dev@archonet.com, gary.stainburn@ringways.co.uk
  Subject: Re: master-detail relationship and count
  In-Reply-To: <200211291035.12923.dev@archonet.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox