これを試してください:
DELETE t
OUTPUT deleted.*
FROM ItemTable t
JOIN (
SELECT DENSE_RANK() OVER (PARTITION BY ItemLabel ORDER BY lt.ItemID DESC, it.id) num
, it.Id
FROM ItemTable it
LEFT JOIN
LinkTable lt ON
lt.ItemId = it.id
) t2 ON t2.Id = t.Id
WHERE num > 1
SQL フィドル
上記のアプローチはあなたの状況に適していますが、より読みやすく、より詳細に制御し、より良い概要を把握できるアプローチをお勧めします。これは、各ステップを分析およびテストできるマルチステップ アプローチです。
-- get ItemLabels of duplicate records
SELECT ItemLabel
INTO #Duplicate_ItemLabels
FROM ItemTable it
GROUP BY
it.ItemLabel
HAVING COUNT(*) > 1
-- get ItemLabels of duplicate records that have at least one record related to LinkTable
SELECT *
INTO #Duplicate_ItemLabels_Related_To_LinkTable
FROM #Duplicate_ItemLabels d1
WHERE EXISTS
(
SELECT *
FROM ItemTable it
JOIN Linktable lt ON
lt.ItemID = it.ID
WHERE it.ItemLabel = d1.ItemLabel
)
-- get ItemLabels of duplicate records that don't have any records related to LinkTable
SELECT ItemLabel
INTO #Duplicate_ItemLabels_NOT_Related_To_LinkTable
FROM #Duplicate_ItemLabels
EXCEPT
SELECT ItemLabel
FROM #Duplicate_ItemLabels_Related_To_LinkTable
-- delete unwanted records for ItemLabels that have records related to linkTable
DELETE it
OUTPUT deleted.*
FROM ItemTable it
JOIN #Duplicate_ItemLabels_Related_To_LinkTable dup ON
dup.ItemLabel = it.ItemLabel
WHERE NOT EXISTS
(
SELECT *
FROM Linktable lt
WHERE lt.ItemID = it.ID
)
-- delete unwanted records for ItemLabels that don't have any records related to linkTable
DELETE it
OUTPUT deleted.*
FROM ItemTable it
JOIN #Duplicate_ItemLabels_NOT_Related_To_LinkTable dup ON
dup.ItemLabel = it.ItemLabel
JOIN
(
-- records deleted will be all those that have ID greater than the smallest ID for this ItemLabel
SELECT ItemLabel
, MIN(ID) ID
FROM ItemTable dup
GROUP BY
dup.ItemLabel
) gr ON
gr.ID < it.ID
AND gr.ItemLabel = dup.ItemLabel
-- if after these DELETEs there are still duplicate records, it
-- means that there are records for same ItemLabel with
-- different ID and all of them are related to LinkTable
簡単に変更し、結果をテストし、削除するレコードを操作できます。SQL Fiddleを作成し、データのさまざまなサンプルを入れて、データがどのように処理されているかを確認できるようにしました。
2 番目のアプローチのデータをサンプリングするために、複数のレコードが関連してItemTable
いる同じレコードを追加しました(任意に削除されるレコードはありません)。ItemLabel
ID
LinkTable