From efd509d3ac84369bba003c2a01665ade4a6a18c4 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Thu, 5 Mar 2026 14:53:28 +0530 Subject: [PATCH] Fix MariaDB aggregate alias error in store stock ratio query --- app/Controllers/Medical.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/Controllers/Medical.php b/app/Controllers/Medical.php index 7bd1c44..dedb2b4 100644 --- a/app/Controllers/Medical.php +++ b/app/Controllers/Medical.php @@ -8914,8 +8914,12 @@ class Medical extends BaseController JOIN purchase_invoice_item s ON p.id=s.item_code WHERE " . implode(' AND ', $ratioConds) . " GROUP BY p.id, p.item_name - HAVING current_unit_qty > 0 - ORDER BY (current_unit_qty / GREATEST(sale_qty_30,1)) DESC, current_unit_qty DESC + HAVING SUM(s.total_unit-s.total_sale_unit-s.total_lost_unit-s.total_return_unit) > 0 + ORDER BY ( + SUM(s.total_unit-s.total_sale_unit-s.total_lost_unit-s.total_return_unit) + / GREATEST(SUM(CASE WHEN s.stock_date >= DATE_ADD(CURDATE(), INTERVAL -30 DAY) THEN IFNULL(s.total_sale_unit,0) ELSE 0 END),1) + ) DESC, + SUM(s.total_unit-s.total_sale_unit-s.total_lost_unit-s.total_return_unit) DESC LIMIT 10"; $ratioRows = $this->db->query($ratioSql, $ratioParams)->getResultArray();