Browse Source

fix: region status

igb 1 day ago
parent
commit
555059ed2e
1 changed files with 14 additions and 3 deletions
  1. 14 3
      statistics_region.php

+ 14 - 3
statistics_region.php

@@ -88,19 +88,30 @@ function getOrdersByRegion($conn, $start_date, $end_date, $employee_filter = nul
     
     $sql = "SELECT 
                 c.countryName, 
-                COUNT(o.id) as order_count,
+                COUNT(DISTINCT o.id) as order_count,
                 SUM(o.total_amount) as total_amount,
-                SUM(oi.quantity) as total_quantity
+                (SELECT SUM(oi_sub.quantity) 
+                 FROM order_items oi_sub 
+                 JOIN orders o_sub ON oi_sub.order_id = o_sub.id
+                 JOIN customer cu_sub ON o_sub.customer_id = cu_sub.id
+                 WHERE o_sub.order_date BETWEEN ? AND ?
+                 AND cu_sub.cs_country = cu.cs_country
+                 $employee_condition) as total_quantity
             FROM orders o
             JOIN customer cu ON o.customer_id = cu.id
             JOIN country c ON cu.cs_country = c.id
-            LEFT JOIN order_items oi ON o.id = oi.order_id
             WHERE o.order_date BETWEEN ? AND ?
             $employee_condition
             GROUP BY cu.cs_country
             ORDER BY total_quantity DESC
             LIMIT 10";
     
+    // 添加额外的参数用于子查询
+    $params = array_merge([$start_date, $end_date], $params);
+    $types = "ss" . $types;
+    
+
+
     $stmt = $conn->prepare($sql);
     $stmt->bind_param($types, ...$params);
     $stmt->execute();