删除 sql 中的组列表
deleting list of groups in sql
在我的查询中
SELECT
[Dt],
[ItemRelation],
[DocumentNum],
[DocumentDate],
[CalendarYear]
FROM
[Action].[dbo].[testtable]
这些列正在分组:
[ItemRelation]
[DocumentNum]
[CalendarYear]
我必须删除的任何组。
所以这里有数据供查询
DECLARE @LIST_ABOVE TABLE (ItemRelation NVARCHAR(10),
DocumentNum NVARCHAR(10),
CalendarYear INT)
INSERT INTO @LIST_ABOVE (ItemRelation, DocumentNum, CalendarYear)
VALUES
(11511,5,2017),
(11628,2,2017),
(11661,163,2017),
(11692,82,2017),
(11709,143,2017),
(13189,33,2017),
(13284,2,2017),
(158009,12,2017),
(158121,63,2017),
(11514,60,2017),
(11628,3,2017),
(11671,13,2017),
(11706,8,2017),
(11741,163,2017),
(13191,7,2017),
(13284,3,2017),
(158010,12,2017),
(158122,41,2017),
(11592,33,2017),
(11628,140,2017),
(11683,70,2017),
(11706,50,2017),
(13163,70,2017),
(13191,33,2017),
(13322,4,2017),
(158010,89,2017),
(158122,62,2017),
(11594,9,2017),
(11633,75,2017),
(11683,140,2017),
(11706,51,2017),
(13163,75,2017),
(13250,83,2017),
(13322,36,2017),
(158010,95,2017),
(158122,63,2017),
(11623,71,2017),
(11634,154,2017),
(11683,154,2017),
(11706,58,2017),
(13163,131,2017),
(13269,50,2017),
(157186,57,2017),
(158121,41,2017),
(11626,29,2017),
(11661,143,2017),
(11683,163,2017),
(11709,81,2017),
(13189,13,2017),
(13269,66,2017),
(157192,56,2017),
(158121,62,2017)
当我运行这个脚本
DELETE T FROM [Action].[dbo].testtable T
WHERE EXISTS (SELECT 1
FROM @LIST_ABOVE
WHERE T.[ItemRelation] = [ItemRelation]
AND T.[DocumentNum] = [DocumentNum]
AND T.[CalendarYear] = [CalendarYear]);
从 table 开始,具有上述组的行未被删除。
每组有 40 行。
所以必须删除40*52=2080行
如何删除包含该组的行?
我只是不想手动操作。但是我不能删除它们。
首先,您已将 ItemRelation 和 DocumentNum 声明为 NVARCHAR(10),但将它们作为整数插入。这本身不是问题,但如果 testtable 中的数据类型是数字,则数据类型不匹配可以解释它。
DELETE T
FROM [Action].[dbo].testtable AS T
INNER JOIN @LIST_ABOVE AS LA
ON LA.ItemRelation = T.ItemRelation
AND LA.DocumentNum = T.DocumentNum
AND LA.CalendarYear = T.CalendarYear
如果这不起作用,那么我建议尝试删除@LIST_ABOVE
中的第一项
DELETE FROM [Action].[dbo].testtable AS T
WHERE T.ItemRelation = 11511
AND T.DocumentNum = 5
AND T.CalendarYear = 2017
最后你需要确定为什么没有发生删除。数据类型不匹配似乎是最有可能的,数据只是不存在于 [Action].[dbo].testtable 是另一种可能性。
在我的查询中
SELECT
[Dt],
[ItemRelation],
[DocumentNum],
[DocumentDate],
[CalendarYear]
FROM
[Action].[dbo].[testtable]
这些列正在分组:
[ItemRelation]
[DocumentNum]
[CalendarYear]
我必须删除的任何组。
所以这里有数据供查询
DECLARE @LIST_ABOVE TABLE (ItemRelation NVARCHAR(10),
DocumentNum NVARCHAR(10),
CalendarYear INT)
INSERT INTO @LIST_ABOVE (ItemRelation, DocumentNum, CalendarYear)
VALUES
(11511,5,2017),
(11628,2,2017),
(11661,163,2017),
(11692,82,2017),
(11709,143,2017),
(13189,33,2017),
(13284,2,2017),
(158009,12,2017),
(158121,63,2017),
(11514,60,2017),
(11628,3,2017),
(11671,13,2017),
(11706,8,2017),
(11741,163,2017),
(13191,7,2017),
(13284,3,2017),
(158010,12,2017),
(158122,41,2017),
(11592,33,2017),
(11628,140,2017),
(11683,70,2017),
(11706,50,2017),
(13163,70,2017),
(13191,33,2017),
(13322,4,2017),
(158010,89,2017),
(158122,62,2017),
(11594,9,2017),
(11633,75,2017),
(11683,140,2017),
(11706,51,2017),
(13163,75,2017),
(13250,83,2017),
(13322,36,2017),
(158010,95,2017),
(158122,63,2017),
(11623,71,2017),
(11634,154,2017),
(11683,154,2017),
(11706,58,2017),
(13163,131,2017),
(13269,50,2017),
(157186,57,2017),
(158121,41,2017),
(11626,29,2017),
(11661,143,2017),
(11683,163,2017),
(11709,81,2017),
(13189,13,2017),
(13269,66,2017),
(157192,56,2017),
(158121,62,2017)
当我运行这个脚本
DELETE T FROM [Action].[dbo].testtable T
WHERE EXISTS (SELECT 1
FROM @LIST_ABOVE
WHERE T.[ItemRelation] = [ItemRelation]
AND T.[DocumentNum] = [DocumentNum]
AND T.[CalendarYear] = [CalendarYear]);
从 table 开始,具有上述组的行未被删除。 每组有 40 行。
所以必须删除40*52=2080行
如何删除包含该组的行?
我只是不想手动操作。但是我不能删除它们。
首先,您已将 ItemRelation 和 DocumentNum 声明为 NVARCHAR(10),但将它们作为整数插入。这本身不是问题,但如果 testtable 中的数据类型是数字,则数据类型不匹配可以解释它。
DELETE T
FROM [Action].[dbo].testtable AS T
INNER JOIN @LIST_ABOVE AS LA
ON LA.ItemRelation = T.ItemRelation
AND LA.DocumentNum = T.DocumentNum
AND LA.CalendarYear = T.CalendarYear
如果这不起作用,那么我建议尝试删除@LIST_ABOVE
中的第一项DELETE FROM [Action].[dbo].testtable AS T
WHERE T.ItemRelation = 11511
AND T.DocumentNum = 5
AND T.CalendarYear = 2017
最后你需要确定为什么没有发生删除。数据类型不匹配似乎是最有可能的,数据只是不存在于 [Action].[dbo].testtable 是另一种可能性。