activerecord - Get the result of SUM and group field from Active record in Codeigniter -
i need build query in codeigniter don't know how result of sum:
select description, sum(amount) payment (date_payment between '2014-02-01 00:00:00' , '2014-02-28 23:59:59') group description;
i'm trying result this:
$qwer = $this->db->query(" select description, sum(amount) payment (date_payment between '$min_date' , '$max_date') group description;"); $i = 0; foreach ($qwer->result() $payment) { $det_payment[$i] = array( 'description'=>$payment->description, 'amount'=>$payment->amount ); $i++; }
of course "$payment->amount" wrong, if use alias sum, model doesn't work.
edit: right can choose between description or sum, can't use both select , select_sum
$this->db->select('description'); //$this->db->select_sum('amount', 'amount'); $this->db->where('date_payment >=', $min_fecha); $this->db->where('date_payment <=', $max_fecha); $this->db->group_by("description"); $qwer = $this->db->get('payment');
setting false second parameter, 'select' allows write custom sentence.
$this->db->select('description, sum(amount) amount', false); $this->db->where('date_payment >=', $min_date); $this->db->where('date_payment <=', $max_date); $this->db->group_by("description"); $qwer = $this->db->get('payment');
Comments
Post a Comment