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!

Delete Date that is Older 1

Status
Not open for further replies.

Compkitty

Programmer
Jan 7, 2005
121
0
0
US
I want to be able to find which date is older between two rows w/ the same data.. Except the date(s).. One should be odler than the other and I want to be able to delete the one that is older... I'm not sure of a date function to do that...
 
This probably can be easily done with MAX() aggregate. Post sample data and results you want.

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
SAMPLE DATA

460 561 522 A 7635 FL E 2005-05-13 00:00:0
460 561 522 A 7635 FL M 2005-05-14 00:00:00

SHOULD DELETE The (E) Record.. and leave
460 561 522 A 7635 FL M 2005-05-14 00:00:00

However, I have a table that has 51 records that are duped like this..
 
Code:
delete table1
from table1 t1
where DateField < (select max(DateField)
                   from table1 t2
                   where t1.fld1 = t2.fld2
                     and t1.fld2 = t2.fld2
                     and...)

The only problem is that if you have 2 rows exactly the same (with the same date) and they are the newest ones - this woul not delete them. Do you have a case like this?

"A long life is when each day is full and every hour wide!"
(A Jewish Mentor)
M. [hourglass]

 
Something like this should do it:

Code:
DELETE table_name
FROM table_name t
WHERE date_col <> (
    SELECT MIN(date_col)
    FROM table_name
    WHERE col1 = t1.col1
      AND col2 = t1.col2
      AND col3 = t1.col3
      ...
  )

--James
 
Sorry, I should have used the MAX function in that subquery obviously...

--James
 
Hey, James...
Bit you on this one...
[tongue]


"A long life is when each day is full and every hour wide!"
(A Jewish Mentor)
M. [hourglass]

 
You guys have been SOOOO helpful and i'm learning sooo much that I (obviously) didn't know or wasn't even thinking of...
 
Hey, Compkitty - thanks for the star! [love2]

It's OK, James - I am in the north - so this is my late morning... just kidding... and since you advanced me when I just woke up... ---> [peace]

"A long life is when each day is full and every hour wide!"
(A Jewish Mentor)
M. [hourglass]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top