Click here to Skip to main content
15,906,097 members

Comments by Andrw_S (Top 11 by date)

Andrw_S 8-Sep-15 9:44am View    
The result is exactly as I would expect - i.e. the "true" page is loaded.
Andrw_S 27-Apr-15 4:02am View    
This is not a solution to the original condition, however I shall add this in case it happens to be of use to anyone in a similar spot.

If you also have a primary ID column in there, then the below works:

Select
m.ID,
m.UserName,
l.Date,
l.Time,
l.LoginStatus
From LoginStatus l
inner join
(
Select
max(LoginStatusID) [ID],
UserName
From LoginStatus
group by
UserName
) m
on l.LoginStatusID=m.ID and l.UserName=m.UserName
Order by
m.UserName
Andrw_S 27-Apr-15 3:54am View    
Hi Pratap420,

Apologies for the late reply. Thank you very much.
That works fine!

NOTE: I had to make a small alteration to your code for the "desc" condition on "_date", thus:

Select UserName,_date,_time,LoginStatus
from
( Select * ,ROW_NUMBER() over (partition by username order by _date desc,_time desc ) rk
from @t) A
where rk=1


I'm sure this was just an omission
Andrw_S 24-Apr-15 9:11am View    
Hi,
Yes, I did.
It's very similar to:
http://stackoverflow.com/questions/1049702/create-a-sql-query-to-retrieve-most-recent-records
Andrw_S 24-Apr-15 8:43am View    
Hi,
So if you put together a query that looks something like this:

Select
UserName,
MAX(CAST([Date] AS datetime) + CAST([Time] AS datetime)) [Last Entry],
LoginStatus
from LoginStatus
Group By
UserName,
LoginStatus
order by
UserName

Then you end up getting TWO results per person. One each for loginStatus=1 and loginStatus=0.

I've got this far with several queries, but not managed that last step of retrieving only the most recent record.
Thanks.