| From: | Patrick JACQUOT <patrick(dot)jacquot(at)anpe(dot)fr> |
|---|---|
| To: | |
| Cc: | SQL PostgreSQL MailList <pgsql-sql(at)postgresql(dot)org> |
| Subject: | Re: Need SQL Help Finding Current Status of members |
| Date: | 2005-12-16 10:12:20 |
| Message-ID: | 43A29304.9050801@anpe.fr |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-sql |
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)
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Michael Avila | 2005-12-16 13:46:03 | Re: Need SQL Help Finding Current Status of members |
| Previous Message | Richard Huxton | 2005-12-16 09:24:22 | Re: Need SQL Help Finding Current Status of members |