Question re 2 aggregates from 1 query

From: Scott Ribe <scott_ribe(at)killerbytes(dot)com>
To: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Question re 2 aggregates from 1 query
Date: 2009-06-18 17:49:27
Message-ID: C65FDA47.BAE0D%scott_ribe@killerbytes.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Assume a simple many-to-one between 2 tables:

create table docs (id int8 primary key, timestamp imported_when);
create table pages (id int8 primary key, doc_id int8 not null references
docs);

It's easy to get a count of docs imported by date:

select imported_when::date, count(1)
from docs
group by imported_when::date;

It's easy to get a count of pages imported by date:

select imported_when::date, count(1)
from docs, pages
where docs.id = pages.doc_id
group by imported_when::date;

Is there any way to get count of docs & pages imported by date without
resorting to selecting from a select:

select dt, count(1), numpgs from (
select docs.imported_when::date as dt, count(1) as numpgs
from docs, pages
where docs.id = pages.doc_id
group by docs.imported_when::date, docs.id
) as t1 group by dt order by dt;

--
Scott Ribe
scott_ribe(at)killerbytes(dot)com
http://www.killerbytes.com/
(303) 722-0567 voice

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Scott Ribe 2009-06-18 17:54:15 Re: Question re 2 aggregates from 1 query
Previous Message Mike Christensen 2009-06-18 17:20:49 Re: SET TIMEZONE doesn't affect to SELECT statement