Sunday, March 25, 2012

Distinct problem - how can I sort this?

Hi All

I have a table sent to me from another source over which i have no control of the content (they don't sem to be able to get a distinct instance of persons for some reason? - anyway that is out of my control)
Now this table contains multiple instances of persons, each person has a unique identifier plus a set of dates and a field that indicates the state of that person (can be 'Curr' or 'Ex').

Fields are as follows:-

PersonID - char(10) - This is unique identifier for each person
Date1 - DateTime
P_State - char(20)

Sample of data from table1:-
-
PersonID - Date1 - P_State
A11324 1998-04-21 Curr
A11324 1999-05-01 Ex
A11324 1998-07-12 Ex
A11324 1998-05-23 Curr
B44321 1999-07-01 Curr
B11111 1999-07-01 Ex
B11111 1998-03-01 Curr
B22222 1999-03-31 Ex
B22222 1998-04-11 Curr
B33333 2004-03-10 Curr
B44443 2002-09-01 Curr
-

What I need to get is the latest instance of each person and their present p_state, I've tried many ways but can't seem to get a distinct instance of the latest record for each person, can anyone point me in the right direction please.

thanks.

select personid,max(date1),p_state

from table1

group by personid,p_state

|||thanks
but that doesn't give me a single instance of each person?

It's much the same as

select personid,date1,p_state
from table1
order by personid,date1

is it not ?

edit
Sorted now thanks, I removed the p_state in your query and then used the output as a basis for another query.

Thanks.

No comments:

Post a Comment