Solved

Getting only top one most recent unique row

Posted on 2013-06-21
5
298 Views
Last Modified: 2013-06-21
I want to return the top most recent row containing a distinct modId, eliminating the duplicates.

I have a table:

id       modId
1           1
2            1
3            2
4            3
5            3
6            7
7            8
8            7


Notice the id is the unique id field of the table.
But the Modid can have multiple rows. I want to sort by the Id field descending in order to get the most recent row (yes, there is more data per row not included) and exclude the duplicate modids.

So my return results should be:

8,7,5,3,2 and the less recent duplicate rows will not be included.

id       modId
2            1
3            2
5            3
7            8
8            7

I only need to return the Id field, not the modId field if that helps, because my final query can include

select * from mytable where id IN (select id from yourquery)

How would I do that?

thanks!
0
Comment
Question by:Starr Duskk
5 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 450 total points
ID: 39266816
try the below code

select max(id),modId from mytable  group by modID

Open in new window

0
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 50 total points
ID: 39266832
Neo is correct, just adding a column name for the aggregate:

select max(id) as ID, modId from mytable  group by modID
0
 
LVL 5

Expert Comment

by:DOSLover
ID: 39266888
I think we need to associate the subquery to the main table as follows:
select * from mytable 
  where id IN 
         (select max(b.ID ) from mytable b where b.modId = a.modId group by modId)
    

Open in new window

0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 39267139
I'll give them a shot and let you know! thanks!
0
 
LVL 2

Author Comment

by:Starr Duskk
ID: 39267196
thanks all! the first two worked!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help how to find where my error is in UFD 6 47
Sql query with where clause 2 45
Stored Proc - Rewrite 42 73
Using this function 4 42
Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question