Hi folks,
I've got a game which dumps state information to a database. Eg. Connected, Lost Connection, Grabbed Screen Dump, etc.
Each state is defined by a field called StateTypeId
, which is a foreign key to a simple lookup table 'StateTypes'
.
Now, i'm trying to find all the people who are currently 'Connected
' (eg. StateTypeId == 1), and i'm not sure how to do it. When i say currently connected, I mean that a user doesn't have a 'LostConnection
' state (ie. StateType == 2) as their most recent entry.
I was thinking along the lines of.
SELECT UserId
FROM UserData
WHERE StateType = 1
GROUP BY UserId
or
SELECT UserId
FROM UserData
WHERE StateType != 1
GROUP BY UserId
but this returns all the results, not just the most recent stuff.
Any suggestions?
(i'm guessing I'm missing a TOP(1)
and some rank/order by stuff?)
EDIT
DOH! i forgot to mention, i DO have a DateTime column in the table -> DateEntered.
EDIT 2 - Clarification of question AND some sample data.
I've tried the suggested solutions and i'm not having much luck. As such, i'll provide some sample data to see if this helps clarify my question.
ID State UserName DateAdded
1 Connected Foo 1/1/2000 12:00
2 Connected Bar 1/1/2000 12:01
3 ScreenShot Foo 1/1/2000 12:05
4 LostConnection Foo 1/1/2000 12:06
5 Connected Joe 1/1/2000 12:10
6 LostConnection Joe 1/1/2000 12:15
7 Screenshot Bar 1/1/2000 12:16
8 Connected Jane 1/1/2000 12:22
So looking at this, the people who are connected (currently) are Bar and Jane. Foo and Joe are gone. Also note, Bar's last activity a screenshot, meaning, his last activity was not a LostConnection state.
HTH.