2

Let's say we have this data:

rc_id      phonenumber
1               0
2               0
5               1
5               3
5               3
5               3 

and I want to delete records matching rc_id of 5 and phonenumber of 3 while keeping at least 1 of them

The final result should be:

rc_id      phonenumber
1               0
2               0
5               1
5               3

Note: I don't want to use an unique identifier

I'm using this statement at the moment, but I noticed this deletes all other duplicates as well.

cmd.CommandText = "DELETE FROM tbl_data WHERE rc_id='5' AND phonenumber='3'";
xperator
  • 2,743
  • 7
  • 34
  • 58
  • That question does not apply to SQLite. – CL. Aug 05 '13 at 12:28
  • 1
    take a look at this http://stackoverflow.com/questions/8190541/deleting-duplicate-rows-from-sqlite-database – Ehsan Aug 05 '13 at 12:39
  • @xperator have you tried the above link? – Ehsan Aug 05 '13 at 12:48
  • @EhsanUllah I just read that and I kinda understand how it works. But the statements used in there doesn't have a `WHERE` clause. It used a `GROUP BY` which I don't know how it works. I think the OP in there wants to delete any duplicates in the entire DB – xperator Aug 05 '13 at 13:21

3 Answers3

0

You could potentially use RowNumber() over()

delete from tbl_data where  Rownumber() Over(partition by rc_id, phonenumber Order by rc_id) > 1
RoughPlace
  • 1,111
  • 1
  • 13
  • 23
-1

set rowcount 1 delete from tbl_data where rc_id='5' AND phonenumber='3'

Nisarg Shah
  • 354
  • 1
  • 14
-1

What about this:

cmd.CommandText = "DELETE FROM tbl_data WHERE rc_id='5' AND phonenumber='3'
                   LIMIT (SELECT COUNT(*)-1 FROM tbl_data WHERE rc_id='5' AND phonenumber='3')";
Rob
  • 919
  • 7
  • 16