本帖最后由 zez 于 2012-6-20 08:14 编辑
查询1改为:
SELECT CC.编号, CC.名称, CC.批量, IIf(Right([批量],1)='3',[余额],[d]) AS 入库
FROM (SELECT BB.编号, BB.名称, BB.入库 as d, BB.批量, BB.批号, DSum("入库","BB","名称='" & [名称] & "' and 批号<='" & [批号] & "' and right(批量,1) <>'" & 3 & "'") AS 余额
FROM (SELECT A.编号, A.名称, A.入库, A.批号 & "-1" AS 批量,A.批号
FROM A
UNION ALL SELECT B.编号, B.名称, -B.出库, B.批号 & "-2" AS 批量, B.批号
FROM B
UNION ALL SELECT aa.编号, aa.名称, Sum(aa.入库) AS 入库之总计, [批号] & "-3" AS 表达式1, [批号]
FROM (SELECT A.编号, A.名称, A.入库, A.批号
FROM A
UNION ALL SELECT B.编号, B.名称, -B.出库, B.批号
FROM B
) as aa
GROUP BY aa.编号, aa.名称, [批号] & "-3", [批号]
) as BB
ORDER BY BB.名称, BB.批量) AS CC;
|