Is there a better way?I have a password history table that functions as a fifo queue.create table #hist ( userid int not null, pword varchar(14) not null, dt smalldatetime not null, constraint pk primary key (userid,pword) )goinsert #histselect 1,'oldest','9/11/01' unionselect 1,'middle','9/12/02' unionselect 1,'newest',getdate()insert #histselect 2,'oldest','1/1/00' unionselect 2,'newest',getdate()go
I intend to only remember the last three passwords, so I want to pop (delete) the oldest record, if there are three. If there are {0,1,2} records, I don't want to delete anything. Here is the working query I have come up with..delete hfrom #hist hwhere exists ( select 1 from #hist where h.userid = userid group by userid having count(*) = 3) and dt = ( select min(dt) from #hist where h.userid = userid ) and userid = @useridgo
..but I am wondering if someone has a smarter way to write it so that I don't have to seek on pk 3 times...<O>