Re: SUM() & GROUP BY - Mailing list pgsql-sql

From Richard Huxton
Subject Re: SUM() & GROUP BY
Date
Msg-id 409B8E5F.6090408@archonet.com
Whole thread Raw
In response to Re: SUM() & GROUP BY  ("Martin Kuria" <martinkuria@hotmail.com>)
List 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


pgsql-sql by date:

Previous
From: Jeff Boes
Date:
Subject: Re: not really SQL but I need info on BLOBs
Next
From: "Kornelije"
Date:
Subject: SELECT - ORDER BY Croatian characters ....