What would be wrong with my active record query? The SUM function works fine, but the COUNT() parts return the count of all records (like the conditional expression is always true).
$this->db->select('
t1.account_balance,
t2.CustomerName AS customer,
t2.CustomerId,
SUM(IF(t3.Bargain="Sale",t3.TotalAmount,0)) AS total_sale,
SUM(IF(t3.Bargain="Purchase",t3.TotalAmount,0)) AS total_buy,
COUNT(IF(t3.Bargain="Sale",t3.Bargain,0)) AS count_sale,
COUNT(IF(t3.Bargain="Purchase",t3.Bargain,0)) AS count_buy,
', FALSE);
$this->db->from("balances AS t1");
$this->db->join("customer AS t2","t2.CustomerId = t1.customer_id","left");
$this->db->join("gold_order AS t3","t3.CustomerId = t2.CustomerId","left");
$this->db->group_by("t3.CustomerId");
$object = $this->db->get();
Result:
I want the count_sale to be 3 and the count_buy to be 4.

