Re: Getting the count(*) from two tables and two date ranges in same query

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Håkan Jacobsson <hakan(dot)jacobsson(at)relevanttraffic(dot)com>
Cc: Adam Rich <adam(dot)r(at)sbcglobal(dot)net>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Getting the count(*) from two tables and two date ranges in same query
Date: 2008-01-30 17:35:56
Message-ID: 14318.1201714556@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

=?iso-8859-1?Q?H=E5kan_Jacobsson?= <hakan(dot)jacobsson(at)relevanttraffic(dot)com> writes:
> I just realised that issuing the SQL on one table produces the correct count.

> SELECT sum(case when table2.date between '2007-07-13' and '2007-07-13' then 1 else 0
> end) as sumx FROM table2 WHERE id = n;

> This is working alright.
> So the problem should lie in the last part:

> from table2, table3
> where table2.id = table3.id
> and table2.id = n;

It sounds like there are multiple rows in table3 matching the id of (at
least some of) the rows in table2. You'll get an increment to the sum
for each join pair.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2008-01-30 18:09:27 Re: postgresql book - practical or something newer?
Previous Message Erik Jones 2008-01-30 15:19:30 Re: PGCon vs Postgresql Conference