Re: job for sql, pl/pgsql,gawk,perl or ??

From: Michael Glaesemann <grzm(at)myrealbox(dot)com>
To: Dino Vliet <dino_vliet(at)yahoo(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: job for sql, pl/pgsql,gawk,perl or ??
Date: 2004-08-28 00:59:31
Message-ID: 851A145A-F88D-11D8-A2A0-000A95C88220@myrealbox.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Aug 28, 2004, at 4:20 AM, Dino Vliet wrote:

> For various reasons I sometimes want only these
> customers and reason as follows:
> "Give me the id's of persons wo start with a
> status="yes" and end with a status="yes". Then I can
> track so called "doubters".
>
> How to do this in postgresql?

If I understand your table schema correctly, you're going to want to do
a couple of self-joins on the table (which I shall call "foo"),
something like:

select id
from foo as foo1
join foo as foo2 using (id, prod, fdate)
join foo as foo3 using (id, prod, fdate)
where
foo1.stat = 'yes'
and foo2.stat <> 'yes'
and foo3.stat = 'yes'
and foo2.sdate > foo1.sdate
and foo3.sdate > foo2.sdate;

Something link this will probably work for the yes -> not yes -> yes
situations. However, I think you may also get what you're looking for
with anything that goes from not 'yes' to 'yes'. This covers both the
situation of changing from 'yes' to not 'yes' and back to 'yes' as well
as the case from 'no' to 'yes'.

select id
from foo as foo1
join foo as foo2 using (id, prod, fdate)
where
foo1.stat <> 'yes'
and foo2.stat = 'yes'
and foo2.sdate > foo1.sdate;

Just so you know, this isn't a PostgreSQL-specific issue, but rather an
SQL one. You might want to check out some SQL tutorials on the web or
perhaps pick up a book or two on SQL. I've found Joe Celko's "SQL for
Smarties" helpful.

Good luck!

Michael Glaesemann
grzm myrealbox com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Michael Glaesemann 2004-08-28 01:22:05 Re: Introducing another primary key field?
Previous Message Martijn van Oosterhout 2004-08-28 00:25:15 Re: FK question