How can I get codeigniter a COUNT () or a SUM () of the same value?

0

Well, that ... I'm making an application with Codeigniter, and everything is going very well, but to present a summary of data I'm looking for a solution to get the sum of the number of times that value is positive (but in this case it is literally "yes") within a query with GROUP BY.

The query in my model:

// Consulta para programas
public function get_datos('programas,programas'.'nombre,registros','registros.programa','registros.monto','registros.capacitacion',$limite,$offset) {

    $this->db->select('programas.*,COUNT(programas.nombre) as acciones,SUM(registros.monto) as inversion,COUNT(registros.capacitacion) as capacitados');
    $this->db->from('programas');
    $this->db->join('registros','programas.nombre = registros.programa','left');
    //$this->db->where('registros.capacitacion','si'); 
    $this->db->group_by('programas.nombre');
    $this->db->having('acciones >= 0',null,false);
    $this->db->order_by('programas.nombre','asc');
    $this->db->limit($limite,$offset);
    $consulta = $this->db->get();

    if ($consulta->num_rows() > 0) {
        return $consulta->result();
    }

}

This function allows me to show a list of programs grouping the records inherent to each one, each program has investment data, actions and training. As I mentioned, grouped correctly and shows each program with the number of shares that correspond to each one, adding up their investment amounts, but I can not find how to show the sum of the programs that already have training without discarding the records that they do not have yet.

With a WHERE or a LIKE, I delete the records that have "no" value and I get this:

PROGRAMA   | ACCIONES | CAPACITACIÓN REALIZADA | INVERSIÓN
Programa 1 | 2        | 2                      | $23,500.67
Programa 2 | 0        | 0                      | $0
Programa 3 | 1        | 1                      | $42,570.93

If I delete the WHERE, obviously it only counts all the fields as well as "actions":

PROGRAMA   | ACCIONES | CAPACITACIÓN REALIZADA | INVERSIÓN
Programa 1 | 3        | 3                      | $23,500.67
Programa 2 | 0        | 0                      | $0
Programa 3 | 6        | 6                      | $42,570.93

And what I need is this (program 1 has 3 registered events, of which only 2 have completed their training):

PROGRAMA   | ACCIONES | CAPACITACIÓN REALIZADA | INVERSIÓN
Programa 1 | 3        | 2                      | $23,500.67
Programa 2 | 0        | 0                      | $0
Programa 3 | 6        | 1                      | $42,570.93

I tried some things like IF (COUNT (field), 'yes'), but it seems to not work in Codeigniter.

If anyone has any ideas, I will thank them very much. Greetings.

    
asked by Drewkenobi 15.12.2017 в 08:03
source

0 answers