下面是错误的语句:
SELECT 查询1.产品ID, Sum(查询1.数量之合计) AS 数量之合计之合计, 查询1.仓库 FROM ( (SELECT 入库单.产品ID, Sum(入库单.数量) AS 数量之合计, 入库单.仓库 FROM 入库单 GROUP BY 入库单.产品ID, 入库单.仓库) UNION ALL (SELECT 出库单.产品ID, Sum(出库单.数量)*-1 AS 数量之合计, 出库单.仓库 FROM 出库单 GROUP BY 出库单.产品ID, 出库单.仓库) UNION ALL
(SELECT 报废记录.产品ID, Sum(报废记录.报废数量)*-1 AS 数量之合计, 报废记录.仓库 FROM 报废记录 GROUP BY 报废记录.产品ID, 报废记录.仓库) ) as 查询1 GROUP BY 查询1.产品ID, 查询1.仓库;
下面这段是修正后的语句:
SELECT 查询1.产品ID, Sum(查询1.数量之合计) AS 数量之合计之合计, 查询1.仓库 FROM ( SELECT 入库单.产品ID, Sum(入库单.数量) AS 数量之合计, 入库单.仓库 FROM 入库单 GROUP BY 入库单.产品ID, 入库单.仓库 UNION ALL SELECT 出库单.产品ID, Sum(出库单.数量)*-1 AS 数量之合计, 出库单.仓库 FROM 出库单 GROUP BY 出库单.产品ID, 出库单.仓库 UNION ALL SELECT 报废记录.产品ID, Sum(报废记录.报废数量)*-1 AS 数量之合计, 报废记录.仓库 FROM 报废记录 GROUP BY 报废记录.产品ID, 报废记录.仓库 ) AS 查询1 GROUP BY 查询1.产品ID, 查询1.仓库;
大家发现不同点了吗?最上面的语句,我为了阅读方便,在UNION ALL的那三个表里,都添加了括号,但这其实就是错误的根源.
标签:UNION,查询,Access,仓库,报废,子句,ID,入库,出库 From: https://www.cnblogs.com/yoooos/p/17573702.html