Solved

Getting only top one most recent unique row

Posted on 2013-06-21
5
303 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

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 this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

734 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