pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Avila <Michael.Avila.1@sbcglobal.net>
To: SQL PostgreSQL MailList <pgsql-sql@postgresql.org>
Subject: Re: Need SQL Help Finding Current Status of members
Date: Fri, 16 Dec 2005 19:44:46 -0500
Message-ID: <NBBBLNPHAMCFENDFHIDCGEOBEGAA.Michael.Avila.1@sbcglobal.net> (raw)
In-Reply-To: <NBBBLNPHAMCFENDFHIDCIENFEGAA.Michael.Avila.1@sbcglobal.net>

Just tried it and it returned nothing.

> > Select * from memberstatus A where not exists
> >    (select * from emberstatus B where B.member_id=A.member_id and
> > B.status_date >A.status_date)

Why the WHERE NOT EXISTS?

Here is a copy and paste of my code

$query = "SELECT * FROM memberstatus A WHERE NOT EXISTS (SELECT * from
memberstatus B WHERE B.member_id=A.member_id AND B.status_date >
A.status_date)";

Mike


> -----Original Message-----
> From: Michael Avila [mailto:Michael.Avila.1@sbcglobal.net]
> Sent: Friday, December 16, 2005 8:46 AM
> To: SQL PostgreSQL MailList
> Subject: RE: [SQL] Need SQL Help Finding Current Status of members
>
>
> Interesting. I think I understand that. I have never worked with
> a SELECT within a SELECT (I think that is called a subquery). I
> am guessing that it works its way through the member status
> records until the latest date "floats" to the top (nothing is > than it).
>
> Will that be a problem performance-wise if there are thousands of records?
>
> Thanks for the help.
>
> Mike
>
>
> > -----Original Message-----
> > From: pgsql-sql-owner@postgresql.org
> > [mailto:pgsql-sql-owner@postgresql.org]On Behalf Of Patrick JACQUOT
> > Sent: Friday, December 16, 2005 5:12 AM
> > Cc: SQL PostgreSQL MailList
> > Subject: Re: [SQL] Need SQL Help Finding Current Status of members
> >
> >
> > Richard Huxton wrote:
> >
> > > Michael Avila wrote:
> > >
> > >> I have a table which keeps track of the status of members. In the
> > >> table is
> > >>
> > >> member_id int(8)
> > >> status_code char(1)
> > >> status_date date
> > >> KEY member_id (member_id,status_code,status_date)
> > >>
> > >>
> > >> Each member can have multiple records because a record is added each
> > >> time
> > >> the status changes but the old record is kept for history.
> > >>
> > >> What I want to do is find the latest status for each member.
> > >
> > >
> > > Michael Fuhr has already described on solution, but if you can alter
> > > the table definition then there might be a neater solution.
> > >
> > > Replace "status_date" with "status_expires" and make it a "timestamp
> > > with time zone". Set the expiry to 'infinity' for the current record
> > > and  you then have a simple select to find the most recent.
> > >
> > > If you regularly want to find which record was active on a particular
> > > time you'll want two columns: valid_from and valid_to. This makes it
> > > much easier to find a row for a specific date.
> >
> > There is a standard way :
> >
> > Select * from memberstatus A where not exists
> >    (select * from emberstatus B where B.member_id=A.member_id and
> > B.status_date >A.status_date)
> >
> > ---------------------------(end of broadcast)---------------------------
> > TIP 5: don't forget to increase your free space map settings




view thread (10+ messages)  latest in thread

Message-ID: <NBBBLNPHAMCFENDFHIDCGEOBEGAA.Michael.Avila.1@sbcglobal.net>
Permalink:  ../NBBBLNPHAMCFENDFHIDCGEOBEGAA.Michael.Avila.1@sbcglobal.net/
Also on:    postgresql.org/message-id/NBBBLNPHAMCFENDFHIDCGEOBEGAA.Michael.Avila.1@sbcglobal.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: Michael.Avila.1@sbcglobal.net
  Subject: Re: Need SQL Help Finding Current Status of members
  In-Reply-To: <NBBBLNPHAMCFENDFHIDCGEOBEGAA.Michael.Avila.1@sbcglobal.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox