-1

I have a dataset like:

order_id | order_item_id | category
1        | 1             | book
1        | 2             | pen
1        | 3             | book

now I have to remove the order_item_id and its row that has duplicate value on category columns but still leave 1 of them. How can i achieve that?

3 Answers3

0

delete from mytable where order_item_id not in (select max(order_item_id) from mytable group by order_ID,category)

0

Delete if there is a row with the same order_id and category but with less order_item_id:

delete from orders o
where exists (
  select 1 
  from orders 
  where 
    orders.order_id = o.order_id
    and
    orders.category = o.category
    and 
    orders.order_item_id < o.order_item_id
  );

See the demo

forpas
  • 160,666
  • 10
  • 38
  • 76
0

I would simply do:

delete from t
    where t.order_item_id > (select min(t2.order_item_id)
                             from t t2
                             where t2.order_id = t.order_id and
                                   t2.category = t.category
                            );
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786