Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Get only one record per users.
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Yak Posting Veteran

62 Posts

Posted - 10/29/2013 :  13:26:26  Show Profile  Reply with Quote
I need to create a query that will select only one record out of x number of record per user id.

Sample Data
User ID    | Date       |Status
001        |10/1/2013   |01
001        |10/2/2013   |A1
002        |1/1/2013    |BB
003        |5/8/2013    |EE
003        |4/2/2013    |BB

Expected Result
User ID    | Date       |Status
001        | 10/2/2013  |A1
002        | 1/1/2013   |BB
003        | 5/8/2013   |EE

Edited by - ugh3012 on 10/29/2013 13:27:04

Flowing Fount of Yak Knowledge

6065 Posts

Posted - 10/29/2013 :  13:42:30  Show Profile  Reply with Quote
Here's one way:

select [User ID]
       , [Date]
       , [Status]
from   (
       select [User ID]
              , [Date]
              , [Status]
              , rn = row_number() over (partition by [user id] order by [date] desc)
       from yourtable
       ) d
where  rn = 1

Be One with the Optimizer
Go to Top of Page

Starting Member

10 Posts

Posted - 10/29/2013 :  17:50:26  Show Profile  Reply with Quote
Select distinct UserID
from yourtable
Go to Top of Page

Premature Yak Congratulator

22864 Posts

Posted - 10/30/2013 :  05:33:31  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Originally posted by ushavellala

Select distinct UserID
from yourtable

You need other columns as well. Try the code used by TG. Also read this for more such methods


Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000