Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server : DELETE rows having more than 1 record (NULL)

I have a table with part numbers and associated component part no and location.

Each part_no may have multiple components or not at all.

Here's an example

Part_No | Component_Part_No | Location
-----------------------------------------    
 12345  | NULL              | 001
 50001  | NULL              | 001
 50001  | 4001              | 001
 50001  | 4002              | 001 

The part no 5001 has a NULL value as well as other components. I want to be able to DELETE the row that has a NULL value for part 50001 but keep part no 12345 intact. The issue is that the NULL value for 50001 is not correct in the table and I need to remove those rows.

Any help is appreciated.

Thank you

like image 410
Alex Avatar asked Nov 16 '25 14:11

Alex


2 Answers

DELETE from table
WHERE component_part_no IS NULL
AND part_no in
    (SELECT part_no 
    FROM table
    WHERE component_part_no IS NOT NULL)
like image 152
FJT Avatar answered Nov 19 '25 10:11

FJT


delete from table where Part_No = 50001 and Component_Part_No IS NULL
like image 23
Derek Kromm Avatar answered Nov 19 '25 10:11

Derek Kromm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!