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





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

Reply via email to