Re: Combining data from Temp Tables

From: Andy Colson <andy(at)squeakycode(dot)net>
To: Jeff Herman <hermanj(at)hvpa(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Combining data from Temp Tables
Date: 2012-02-21 18:36:44
Message-ID: 4F43E43C.40506@squeakycode.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


how about

select date, ln, mbrid, ds, (
select sum(ds) from t2
where t2.date >= t1.date and t2.date <= t1.date + '5 days'::interval
and t1.ln = t2.ln
and t1.mbrid = t2.mbrid)
from t1

That'll give you both the plus and minus (in two different columns), but
it might sum up the same row from table2 multiple times so I'm not sure
its correct.

And I'm not sure the date range is correct.

Another way to look at the same thing:

select date, ln, mbrid, dsplus - dsminus
from (
select date, ln, mbrid, ds as dsplus, (
select sum(ds) from t2
where t2.date >= t1.date and t2.date <= t1.date + '5 days'::interval
and t1.ln = t2.ln
and t1.mbrid = t2.mbrid) as dsminus
from t1
) as x
where dsplus - dsminus <> 0

Totally guessing here.

-Andy

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David Johnston 2012-02-21 19:03:58 Re: Combining data from Temp Tables
Previous Message Tom Lane 2012-02-21 18:21:31 Re: Unfamous 'could not read block ... in file "...": read only 0 of 8192 bytes' again