Solved

Deleting all records but most recent date

Posted on 2007-03-27
2
267 Views
Last Modified: 2010-03-20
I have a question on MS SQL 2000.  I am trying to put together a query to delete certain records.  The schema is as follows:

[name][date][item1][item2]...

examples being:

[andrew][2/2/07][meat][cheese]
[bob][1/1/07][milk][eggs]
[bob][1/2/07][milk][cheese]

Some names occur only once, but there are multiple occurances of the same names with different values for each field thereafter.

What I want to do is delete all records except for the record with the most recent date (keeping name as the key).  I want to end up with only one entry for each name.  When I type this out it seems simple, but I can't seem to formulate the right query.

Thanks for the help!
0
Comment
Question by:Ant_Dogg
[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
2 Comments
 
LVL 10

Accepted Solution

by:
lahousden earned 50 total points
ID: 18803708
delete t
from Your_Table as t
left join (select [name], max ([date]) max_date
             from Your_Table
             group by [name]) as m on m.[name] = t.[name] and m.max_date = t.[date]
where m.[name] is null
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 18805748
delete from yourtable
 where exists (select name from yourtable as x
                           where yourtable.name = x.name
                                and yourtable.date < x.date)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Lag Function DateDiff 2 23
Using rowversion for incremental load of datawarehouse. 12 29
What is needed to become a DBA? 7 43
Migrate SQL 2005 DB to SQL 2016 4 15
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

733 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