Jump to content

Group By


georgerobbo

Recommended Posts

I'm currently trying to get a list of all the students and when they last logged in. When a user is authenticated, a session is created and stored in the Sessions table. The primary key is a SHA1 hash of the time they logged in, their internet protocol address and username.

 

In my query, I only want to get the latest Session in the join. I can't seem to achieve this with GROUP BY. How do I go about doing this? At the moment I'm getting duplicate results.

 

SELECT Users.UserID, Users.Firstname, Users.Surname, Sessions.SessionID, Sessions.IPv4, Sessions.LoginStamp, Sessions.LastActivity FROM Users JOIN Students ON Students.UserID = Users.UserID LEFT JOIN SessionsUsers ON SessionsUsers.UserID = Users.UserID LEFT JOIN Sessions ON Sessions.SessionID = SessionsUsers.SessionID ORDER BY Sessions.LastActivity DESC, Users.Surname ASC

 

Users

-----------

UserID

Firstname

Surname

Hash

 

Students

-----------

UserID

House

LeavingYear

SessionsUsers

------------------

UserID

SessionID

 

Sessions

-----------

SessionID

Ipv4

LoginStamp

ExpireStamp

LastActivity

 

 

I have highlighted the duplicate. How do I group by the last Session. (Effectively ordering by the latest LoginStamp).

 

Thanks

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.