以前曾提过这个问题,wwwwa的答案是肯定的,但这个查询运行起来很慢,特别在数据多的时候,请问能否将这个查询改成vba呢?
wwwwa的答案是:
SELECT id, 名称, 数量, 应提数量, 生产日期, IIF(ISNULL(需求数量1),DD,需求数量1) AS 需求数量
FROM (SELECT id, 名称, 应提数量, 生产日期, 库存, 数量, lj, C.DD, lj-C.DD AS ce,
iif(生产日期=(select max(生产日期) from 表1 where c.名称=名称) and lj<提货数量,1,0) AS bj, iif(ce>0,1,0) AS bj1,
iif(bj=0 and bj1=0,应提数量,iif((bj=0 and bj1=1 and ce<0) or (bj=1 and bj1=0),-1*ce+数量,应提数量)) AS 需求数量1
FROM (
select a.*,b.提货数量 AS DD,iif(lj>=提货数量,数量-(lj-提货数量),数量) as 应提数量,
iif(lj-提货数量>=0 and 库存<=数量 and 应提数量<>0,1,0 ) as bz,
iif(a.lj>=提货数量,lj-[提货数量],0) AS 库存
from (
SELECT A.id, IIF(ISNULL(a.名称),b.名称,A.名称) AS 名称, A.数量, A.生产日期, B.提货数量,A.LJ
FROM (select *,(select sum(数量) from 表1 where 生产日期<=a.生产日期 and 名称=a.名称) as lj
from (SELECT * FROM 表1 order by 生产日期) a) AS A RIGHT JOIN 表2 AS b ON a.名称=b.名称
ORDER BY A.名称, 生产日期) AS A
) AS c
WHERE 库存=0 OR Bz=1) ORDER BY 名称, 生产日期
|