我有一张表,其中包含零件编号和相关组件零件号和位置。
每个part_no
可能包含多个组件,或者根本没有。
这是一个例子
Part_No | Component_Part_No | Location
-----------------------------------------
12345 | NULL | 001
50001 | NULL | 001
50001 | 4001 | 001
50001 | 4002 | 001
零件号5001具有NULL值以及其他组件。我希望能够删除部分50001具有NULL值的行,但保持部分号12345不变。问题是表中的50001的NULL值不正确,我需要删除这些行。
感谢任何帮助。
谢谢
答案 0 :(得分:6)
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)
答案 1 :(得分:2)
delete from table where Part_No = 50001 and Component_Part_No IS NULL
答案 2 :(得分:1)
可能有更漂亮的方式来写它,但这应该做你想要的:
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)
答案 3 :(得分:1)
要做到这一点......零件有组件。如果零件没有已知的组件,那么存储关于该零件位置信息(整体)的重要性,如果零件已知组件,没有组件的记录不正确,是吗?
delete parts
from parts
inner join parts selff
on parts.Part_no = selff.Part_no
and selff.Component_Part_No is not null
where parts.Component_Part_No is null
答案 4 :(得分:0)
DECLARE @tab TABLE(Part_No INT,Component_Part_No INT,Location VARCHAR(3))
INSERT INTO @tab
SELECT 12345,NULL,'001' UNION ALL
SELECT 50001,NULL,'001' UNION ALL
SELECT 50001,4001,'001' UNION ALL
SELECT 50001,4001,'001' UNION ALL
SELECT 11111,NULL,'001' UNION ALL
SELECT 11111,5001,'002' UNION ALL
SELECT 22222,NULL,'003' UNION ALL
SELECT 22222,6001,'003' UNION ALL
SELECT 33333,NULL,'003'
SELECT * FROM @tab;
DELETE
FROM @tab
Part_No IN
(
SELECT Part_No
FROM @tab
GROUP BY Part_No
HAVING COUNT(*) > 1
)
AND [@tab].Component_Part_No IS NULL
SELECT * FROM @tab;
SQL小提琴 - http://sqlfiddle.com/#!3/613c1/1