Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 637783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:39:41+00:00 2026-05-13T20:39:41+00:00

Today at work we got into a discussion about which is the best way

  • 0

Today at work we got into a discussion about which is the best way to do a query like this :

For instance lets assume a users table :

tblUsers
ID      = Autoint
Name    = String

and a login table :

tblLogin
ID         = AUtoint
UserID    = Int
IP        = String
Browser   = String
OS        = String
timestamp = DateTime

What would be the most efficient way to list all the users and the last time they logged in (if ever), and provide an output like :

user       | ip     | timestamp | browser | os   |
-------------------------------------------------
Some User  |1.1.1.1 | 12/12/12  | userBA  | win  |
Other User |1.1.1.1 | 12/12/12  | userBA  | win  |
And Other  |null    | null      | null    | null |
Other Yet  |1.1.1.1 | 12/12/12  | userBA  | win  |

Keep in mind that what we want here is to show all users exactly once even if he has never logged in, and only the most recent login (i.e max(timestamp)).

Is there a way to do it in one SQL statement?

We are using MSSQL 2005.

Thanks in advance guys, Jim

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:39:42+00:00Added an answer on May 13, 2026 at 8:39 pm

    Im my opinion, the most readable way uses row_number(). You can use it to number rows, starting with 1 for each user, like:

    select *
    from (
        select u.name, l.ip, l.timestamp, l.browser, l.os,
          row_number() over (partition by u.id order by timestamp desc) rn 
        from tblUsers u
        inner join tblLogin l on u.id = l.userid
    ) sub
    where rn = 1
    

    A filter on rn = 1 gives the latest row per user. A subquery is required because SQL Server 2005 does not allow you to reference a row_number() in a where clause.

    The most efficient way to do this depends on the amount of logins per user. You can find a good explanation of some of the more advanced methods in this blog post.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into an interesting problem at work today. I got a request to
Today I tried to work on an older application and got this message: This
Today I got our svn repository cloned into git. I had some uncommitted work
The accepted answer to this question , and a similar discussion at work today
Problem This question actually came up at work today. We are planning an experiment
). I got into a bit of issues today with the TcpListener. Things are
Today I've tried to get Edit & Continue to work in my solution, which
I got my computer today to do work on a bashscript called MF.sh ..
Today I got this error in two projects on Heroku when I try to
Got this question in an interview today, and its optimized solution stopped me cold

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.