我在Access 2010(accdb)上有这个查询,它完全正常工作:
SELECT b.category_name, a.item_name,
(SELECT COUNT(*) FROM tbl_stock_receiving AS x WHERE x.safe_stock_id = a.ID) AS received,
(SELECT COUNT(*) FROM tbl_stock_issuance AS y WHERE y.stock_receiving_id = a.ID) AS issued,
(received-issued) AS on_hand, a.safe_stock
FROM tbl_safe_stock AS a INNER JOIN tbl_category AS b
ON a.category_id = b.id
ORDER BY a.item_name;
现在,我需要修改它以包含一个简单的WHERE语句
...
ON a.category_id = b.id
WHERE a.safe_stock > on_hand
ORDER BY a.item_name;
...
当我运行查询时,Access会不断弹出
的参数值on_hand
on_hand是一个别名,你可以在
上看到(received-issued) AS on_hand
我的查询可能有什么问题?
答案 0 :(得分:2)
您已将on_hand
定义为输出列的名称,但SQL解析器不够聪明,无法返回您的查询和“逆向工程”,其中{{ 1}}来自。因此,您需要使用类似on_hand
的内容,但它们也是输出列的别名。
因此,尝试将整个事件作为子查询包装起来,然后应用WHERE a.safe_stock > (received - issued)
和WHERE
子句:
ORDER BY
上面的建议导致“查询过于复杂”错误,所以我的下一个建议是将原始查询保存为[StockCheckBaseQuery],然后执行
SELECT * FROM
(
SELECT b.category_name, a.item_name,
(SELECT COUNT(*) FROM tbl_stock_receiving AS x WHERE x.safe_stock_id = a.ID) AS received,
(SELECT COUNT(*) FROM tbl_stock_issuance AS y WHERE y.stock_receiving_id = a.ID) AS issued,
(received-issued) AS on_hand, a.safe_stock
FROM tbl_safe_stock AS a INNER JOIN tbl_category AS b
ON a.category_id = b.id
)
WHERE safe_stock > on_hand
ORDER BY item_name
这似乎是成功的。