我正在运行查询以找出我的数据库中移动缓慢的股票,但它给出了一个错误,指出按条件无效使用分组。逻辑是我想找出过去 120 天的总销售额少于该项目截至日期的总库存的 5% 的项目。以下是查询:-
SELECT
itemmaster.item as item,
stockbalance.qty as total_stock,
SUM(saleitems.saleqty) as total_sales
FROM itemmaster
JOIN stockbalance ON stockbalance.item = itemmaster.item
JOIN saleitems ON saleitems.item = itemmaster.item
WHERE saleitems.saledate BETWEEN DATE_SUB(NOW(),INTERVAL 120 DAY) AND NOW()
AND (SUM(saleitems.saleqty)) < (stockbalance.qty * 0.05)