tags:

views:

67

answers:

2

Say i have the following table:

id Name Status Date
1 John Working 11/11/2003
2 John Working 03/03/2004
3 John Quit 04/04/2004
4 John Quit 04/05/2004
5 John Quit 04/06/2004
6 Joey Working 03/05/2009
7 Joey Working 02/06/2009
8 Joey Quit 02/07/2009
9 Joey Quit 02/08/2009
10 Joey Quit 02/09/2009

I want to get the date when the change between working and quit occured, so that i get:

3 John Quit 04/04/2004
8 Joey Quit 02/07/2009

How would i do that?

+2  A: 
select ID, Name, Status, Date
from tableName
where ID in (
    select min(ID)
    from tableName
    where Status = 'Quit'
)

This will get the first record that has the status of 'Quit' for each person. It is not necessarily the chronologically first record though. If that is what you're are looking for let me know. Also, if so, can you be sure there won't be any duplicated names; this could be problematic if there are two different John's or Joey's.

Jason
Ok, i posted the exmple wrong, what about now?
The dates don't match up? Do you want the date they last listed as working or the date that goes along with when they quit? In your results you have john's date as the quit date, and joey's date as the record before they quit (not even the latest date chronologically). Please explain further what you would like.
Jason
Argh, bad example again.The thing is there might be more than one quit entry, so i cant just select all the ones with status quit, since i would get multiple records that way. I need the first record where the status was changed from Work to Quit.
the one with the earliest date?
Jason
yes! the one with the earliest date...
Not working... I'm getting data all entries for the first ID only...
+1  A: 
select id, name, date 
from tableName WHERE date = 
(select MIN(date) 
             FROM tableName 
             WHERE NAME='<name>' AND date >= (select MAX(date) 
             FROM tableName WHERE NAME='<name>' AND status = 'Working')
)

Hopefully I didn't screw things up badly in the nested query there. This would only give you 1 user, you could wrap it in a function and call it passing the username or do something like make a temp table with distinct name inserted into it and loop through those names.

A lot of options exist for bringing it form just one user to all users.

sparks
No, this is getting only one record?
Thats what I said, it is only 1 record change. You can call it for each user you want or select distinct users into a temp table then loop through all the temp table records to get it for all users.
sparks
oh ok, but this is not relevant per name, in fact, i dont need it in the query. The only 2 records i need are id and date.
Unless you can always guarantee that the names won't be mixed won't you need to query by name to guarantee that you don't catch John Working -> Joe Quit in a transition? The name makes sure that you're finding a status change of 1 person not just the most recent quit status to any other recent working status.
sparks