我需要删除运行总计<100且[产品系列]包含“电子产品”的记录。
下面是表数据,这里 RunningTotal 不是表中的原始字段,它是子查询(内部查询)输出。
ID Product Line Qty RunningTotal
1 Electronics 15 15
2 R.K. Electricals 20 20
3 Samsung Electronics 10 25
4 Electricals 30 50
5 Electricals 45 95
6 Electronics Components 18 43
7 Electricals 25 120
8 Electronics 50 93
9 Electricals Machines 65 185
10 Electronics 15 108
11 ABC Electronics Ltd 52 160
12 Electricals 15 200
我使用以下查询过滤了[产品系列]包含“电子产品”且运行总计<100的记录。
select t.*, q.runningtotal from salesdata t inner join
(
select t.id,
(
select sum(u.qty)
from salesdata u
where u.[product line] like "*Electronics*" and u.id <= t.id
) as runningtotal
from salesdata t
) q on t.id = q.id
where q.runningtotal < 100 and t.[product line] like "*Electronics*"
我将匹配的记录(基于上述查询)复制到了另一个工作表中。 复制后,我需要删除匹配的记录(<产品> [产品系列]包含“电子产品”并且运行总计<100 )。我在下面的查询中写道,它引发错误无法从指定表中删除,并且无法正常工作
delete from (
select t.*, q.runningtotal from salesdata t inner join
(
select t.id,
(
select sum(u.qty)
from salesdata u
where u.[product line] like "*Electronics*" and u.id <= t.id
) as runningtotal
from salesdata t
) q on t.id = q.id
where q.runningtotal < 100 and t.[product line] like "*Electronics*")
非常感谢您的帮助。
答案 0 :(得分:1)
您可以尝试:
delete from salesdata sd
where sd2.[product line] like "*Electronics*" and
(select sum(sd2.qty)
from salesdata as sd2
where sd2.[product line] like "*Electronics*" and
sd2.id < sd.id
) < 100;
答案 1 :(得分:0)
在MS Access中,DELETE
必须指定一个表而不是一个查询。假设您保存了SELECT
查询(您应该 为引擎编译最佳执行计划),请考虑一个带有子查询的EXISTS
子句。
delete from salesdata s
where exists (select 1 from mysavedquery mq where s.id = mq.id)