Search Postgresql Archives

Question re 2 aggregates from 1 query

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxxx
http://www.killerbytes.com/
(303) 722-0567 voice



-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux