select count(situacao) as Qtde, sum(valorcausa) as Total_Valor_Causa
from processo
where situacao = "Tramitando";
select count(situacao) as Qtde, sum(valorcausa) as Total_Valor_Causa
from processo
where situacao = "Tramitando";
There are several ways to do this, which I recommend, for leaving the code more organized is this:
$this->db->select('count(situacao) as Qtde, sum(valorcausa) as Total_Valor_Causa');
$this->db->where('situacao', 'Tramitando');
$query = $this->db->get('processo');
Another way is to put the entire query inside query()
, like this:
$this->db->query('select count(situacao) as Qtde, sum(valorcausa) as Total_Valor_Causa
from processo where situacao = "Tramitando"')->get();
I recommend the first because you can add other filters in the query, like:
$this->db->order_by('Total_Valor_Causa', 'DESC');
The above code will add a sort by Total_Valor_Causa
.
To return only one row, you can sort by id
desc
, and limit the query to 1 record, thus the result will be the last record entered;
$this->db->order_by('id', 'DESC');
$this->db->limit(1);