SELECT 
  COUNT(DISTINCT o.order_id) AS total_orders, 
  IFNULL(
    SUM(od.amount * od.price), 
    0
  ) - IFNULL(
    SUM(rp.amount * rp.price), 
    0
  ) AS total_spent 
FROM 
  cscart_orders o 
  JOIN cscart_order_details od ON o.order_id = od.order_id 
  LEFT JOIN cscart_rma_returns r ON o.order_id = r.order_id 
  AND r.status = 'A' 
  LEFT JOIN cscart_rma_return_products rp ON r.return_id = rp.return_id 
  AND rp.product_id = od.product_id 
WHERE 
  o.user_id IN ('0') 
  AND o.timestamp > 1735689600 
  AND o.timestamp < 1767225599 
  AND o.status NOT IN ('D', 'I', 'B', 'F', 'A')

Query time 0.00265

JSON explain

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "0.39"
    },
    "nested_loop": [
      {
        "table": {
          "table_name": "r",
          "access_type": "system",
          "possible_keys": [
            "order_id",
            "status"
          ],
          "rows_examined_per_scan": 0,
          "rows_produced_per_join": 1,
          "filtered": "0.00",
          "const_row_not_found": true,
          "cost_info": {
            "read_cost": "0.00",
            "eval_cost": "0.10",
            "prefix_cost": "0.00",
            "data_read_per_join": "48"
          },
          "used_columns": [
            "return_id",
            "order_id",
            "status"
          ]
        }
      },
      {
        "table": {
          "table_name": "rp",
          "access_type": "system",
          "possible_keys": [
            "PRIMARY"
          ],
          "rows_examined_per_scan": 0,
          "rows_produced_per_join": 1,
          "filtered": "0.00",
          "const_row_not_found": true,
          "cost_info": {
            "read_cost": "0.00",
            "eval_cost": "0.10",
            "prefix_cost": "0.00",
            "data_read_per_join": "808"
          },
          "used_columns": [
            "return_id",
            "product_id",
            "amount",
            "price"
          ]
        }
      },
      {
        "table": {
          "table_name": "o",
          "access_type": "ref",
          "possible_keys": [
            "PRIMARY",
            "timestamp",
            "user_id",
            "status"
          ],
          "key": "user_id",
          "used_key_parts": [
            "user_id"
          ],
          "key_length": "3",
          "ref": [
            "const"
          ],
          "rows_examined_per_scan": 1,
          "rows_produced_per_join": 0,
          "filtered": "6.41",
          "cost_info": {
            "read_cost": "0.25",
            "eval_cost": "0.01",
            "prefix_cost": "0.35",
            "data_read_per_join": "668"
          },
          "used_columns": [
            "order_id",
            "user_id",
            "timestamp",
            "status"
          ],
          "attached_condition": "((`demouniformecomd_db`.`o`.`timestamp` > 1735689600) and (`demouniformecomd_db`.`o`.`timestamp` < 1767225599) and (`demouniformecomd_db`.`o`.`status` not in ('D','I','B','F','A')))"
        }
      },
      {
        "table": {
          "table_name": "od",
          "access_type": "ref",
          "possible_keys": [
            "o_k"
          ],
          "key": "o_k",
          "used_key_parts": [
            "order_id"
          ],
          "key_length": "3",
          "ref": [
            "demouniformecomd_db.o.order_id"
          ],
          "rows_examined_per_scan": 2,
          "rows_produced_per_join": 0,
          "filtered": "100.00",
          "cost_info": {
            "read_cost": "0.03",
            "eval_cost": "0.01",
            "prefix_cost": "0.39",
            "data_read_per_join": "29"
          },
          "used_columns": [
            "order_id",
            "product_id",
            "price",
            "amount"
          ]
        }
      }
    ]
  }
}

Result

total_orders total_spent
0 0.00