Skip site navigation (1) Skip section navigation (2)

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 (view raw or flat)
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

pgsql-sql by date

Next:From: Suller AndrĂ¡sDate: 2004-05-07 14:19:24
Subject: Re: COUNT on a DISTINCT query
Previous:From: KornelijeDate: 2004-05-07 11:51:32
Subject: SELECT - ORDER BY Croatian characters ....

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group