Re: SUM() & GROUP BY

From: Richard Huxton <dev(at)archonet(dot)com>
To: Martin Kuria <martinkuria(at)hotmail(dot)com>
Cc: olly(at)lfix(dot)co(dot)uk, middink(at)indo(dot)net(dot)id, pgsql-sql(at)postgresql(dot)org
Subject: Re: SUM() & GROUP BY
Date: 2004-05-07 13:25:51
Message-ID: 409B8E5F.6090408@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Suller András 2004-05-07 14:19:24 Re: COUNT on a DISTINCT query
Previous Message Kornelije 2004-05-07 11:51:32 SELECT - ORDER BY Croatian characters ....