pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Huxton <dev@archonet.com>
To: Martin Kuria <martinkuria@hotmail.com>
Cc: olly@lfix.co.uk
Cc: middink@indo.net.id
Cc: pgsql-sql@postgresql.org
Subject: Re: SUM() & GROUP BY
Date: Fri, 07 May 2004 14:25:51 +0100
Message-ID: <409B8E5F.6090408@archonet.com> (raw)
In-Reply-To: <Sea2-F17FNpyLAPDVXL0000f045@hotmail.com>
References: <Sea2-F17FNpyLAPDVXL0000f045@hotmail.com>

Martin Kuria wrote:
> 
> Here is what Division table contains:
> 
> SELECT * FROM ser_divisions;
> 
> divisions_name | divisions_id
> ---------------------------------------
> DEE                   |   3131
...


> Now I would like to get to know how each Division answered i.e.
> 
> SELECT s.pd_geo, COUNT(s.pd_geo)
> FROM ser s
> WHERE s.ser_divisions = '3131'
> GROUP BY s.pd_geo;

> divisions_name | pd_geo  | count
> -----------------------------------------------
> DEE                 |   1         |  9
> DEE                 |   2         |  2
> DEE                 |   3         |  6
> DEE                 |   4         |  5

SELECT
   d.divisions_name,
   s.pd_geo,
   COUNT(s.pd_geo)
FROM
   ser_divisions d,
   ser s
WHERE
   d.divisions_id = s.ser_divisions
GROUP BY
   d.divisions_name, s.pd_geo
ORDER BY
   d.divisions_name, s.pd_geo
;

It's called a join, and any good SQL book should cover it.

-- 
   Richard Huxton
   Archonet Ltd



view thread (9+ messages)  latest in thread

Message-ID: <409B8E5F.6090408@archonet.com>
Permalink:  ../409B8E5F.6090408@archonet.com/
Also on:    postgresql.org/message-id/409B8E5F.6090408@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, martinkuria@hotmail.com, olly@lfix.co.uk, middink@indo.net.id
  Subject: Re: SUM() & GROUP BY
  In-Reply-To: <409B8E5F.6090408@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