Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update and leave latest item

Status
Not open for further replies.

JezEling

IS-IT--Management
Mar 22, 2004
127
GB
Hi All,

I have a table which contains a list of item numbers and revisions. I have been asked to remove all but the latest revisions from this table, I have tried to achieve this using a CASE query but to now avail (CASE is not something i've really used before :)). Does anyone have any suggestions has to how I can achieve this?

Many Thanks In Advance

Jez
 
Can you post us some example data so we can see what we're dealing with?

And a table struction too if possible.

Fee.

"The question should be, is it worth trying to do, not can it be done"


 
Table structure

PartNumber | Revision

Sample Data

123.123 | 1
123.123 | 2
123.123 | 3

Where the latest revision would be 3, so revisions 1 and 2 need to be deleted.

Jez
 
You can try something like this

delete from tablename where revision not in (select top 1 revision from tablename order by revision desc)

MS
 
I'm not sure that works to be honest

Ok then, this is my attempt;

Code:
create table JezEling
(
partnumber varchar(15),
revision int
)

and the data looks like this
Code:
partnumber      revision    
--------------- ----------- 
123.123         1
123.123         2
123.123         3
124.124         1
124.124         2
124.124         3
124.124         4

I'd be tempted to add a column to mark for deletion so you can see it before you actually delete it, in whch case
Code:
alter table jezeling
add ForDelete varchar(1)

then create a view to link back to for the update
Code:
create view temp_view as
select
partnumber, max(revision)as Rev from jezeling
group by partnumber
go
update jezeling
set ForDelete = 'Y'
where 
( CONVERT(varchar(50), partnumber) + convert(varchar(50),revision)) not in
(select CONVERT(varchar(50), partnumber) + CONVERT(varchar(50),rev ) from temp_view)
go
select * from jezeling

This means you could then just do
Code:
delete from jezeling where fordelete = 'Y'

Hope this helps.



Fee.

"The question should be, is it worth trying to do, not can it be done"


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top