Hi, I'm new to the database world and beginning to dabble in SQL. I can't get my mind wrapped around the database mindset (I'm a PLC guy), and I'm having a little trouble with my query.
I have two tables with the following columns (simplified):
Login: TimeStmp, MessageText, UserID
ChangeoverFloat: DateAndTime, Val, EventID
I need to extract records showing the last person to login prior to certain changeover events occurring.
I have the following code, but of course it returns a record for each login prior to the specified events.
SELECT b.DateAndTime, c.TimeStamp, c.UserID
FROM CPUpgrade.dbo.Login As c INNER JOIN
CPUpgrade.dbo.ChangeoverFloat AS b ON c.TimeStmp<=b.DateAndTime
WHERE c.MessageText = 'Login' AND b.Val = 1
Needless to say, this returns too many records for each event. I just need to find the single closest login records to each event that's taking place.
Any help would be GREATLY appreciated!