Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
kevinxyx
Starting Member
11 Posts |
Posted - 2011-08-15 : 04:09:36
|
sorry about this, i am really newbie in sql,i want to delete similar records, in the picture8/1/20118/21/20118/22/20118/23/20118/24/2011has 0 tag on recordwhile the same record has 1 tag on iti want to delete the record with 1 tag the same as the record with 0 tag, how to do this?Delete Similar Recordsquote: To delete similar records, i.e. where the records are not the same but one field is the same and only one copy needs to be preserved, try the following SQL:delete T1from MyTable T1, MyTable T2where T1.dupField = T2.dupFieldand T1.uniqueField > T2.uniqueFieldThis will delete all records from the table MyTable which have the same value for the field dupField, leaving that record which has the lowest value in uniqueField.
my problem is something like this, quoted from some sql website, problem is cannot understand it, fact that im still learning sql, appreciate any help thanks. |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2011-08-15 : 05:44:30
|
Hi. I think I get what you want but can you clarify?You want to remove: "Any Row where the [Record] Column = 0 where another row exists with the same [DateLog] values and the [Record] value of that row = 1"Do you also want to remove (is this even possible -- It shouldn't!): "Any row that is a complete duplicate of another row (leaving one row behind)"Here's how you'd do the first one.DELETE ldFROM LogDate AS ldWHERE ld.[Record] = 0 AND EXISTS ( SELECT 1 FROM LogDate AS ld2 WHERE ld2.[DateLog] = ld.[DateLog] AND ld2.[Record] = 1 ) Is there some sort of KEY to this table?Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
|
|
kevinxyx
Starting Member
11 Posts |
Posted - 2011-08-15 : 06:37:42
|
sorry for not making it clear, thanks for your help this one do the trickDELETE ldFROM LogDate AS ldWHERE ld.[Record] = 1 AND EXISTS (SELECT 0 FROM LogDate AS ld2 WHERE ld2.[DateLog] = ld.[DateLog] AND ld2.[Record] = 0)it deleted the same record in datelog where the record = 1 |
|
|
|
|
|
|
|