MYSQL get data grouped in SELECT with IF

3

I have the following SQL code

SELECT e.sigla_equipo,
e.patente_equipo,
IF(j.nombre_jornada = 'NOCHE', CONCAT(c.nombre_conductor, ' ',c.apellidoPat_conductor), '') as noche,
IF(j.nombre_jornada = 'DIA', CONCAT(c.nombre_conductor, ' ',c.apellidoPat_conductor), '') as dia
FROM conductor c
INNER JOIN conductor_equipo ce ON ce.id_conductor = c.id_conductor
INNER JOIN equipo e ON e.id_equipo = ce.id_equipo
INNER JOIN jornada j ON j.id_jornada = ce.id_jornada
INNER JOIN estado_conductor ec ON ec.id_estado_conductor = c.id_estado_conductor 
INNER JOIN base b ON b.id_base = c.id_base
WHERE b.nombre_base = 'Base' AND ec.abrev_estado_conductor != 'DES'

that allows me to obtain a truck, its patent and its two drivers but in two rows, as follows:

What I want is that the result be seen only in a row. I tried a GROUP BY for e.id_equipo, but the driver does not appear at night.

The conductor_team table is:

Applying Group By is like this:

    
asked by Alejandro Vega 19.10.2018 в 16:54
source

2 answers

1

Your group by is the solution to the problem.

As you already managed to obtain the data you wanted, in different registers but with the same "keys", the only thing that you have to do is to pass that to a new query, which makes group by .

Suppose we call your current query A.

Then, you could do the following:

Select 
  A.sigla_equipo,
  A.patente_equipo,
  max(A.noche),
  max(A.dia)
from (SELECT e.sigla_equipo,
        e.patente_equipo,
        IF(j.nombre_jornada = 'NOCHE', CONCAT(c.nombre_conductor, ' ',c.apellidoPat_conductor), '') as noche,
        IF(j.nombre_jornada = 'DIA', CONCAT(c.nombre_conductor, ' ',c.apellidoPat_conductor), '') as dia
        FROM conductor c
        INNER JOIN conductor_equipo ce ON ce.id_conductor = c.id_conductor
        INNER JOIN equipo e ON e.id_equipo = ce.id_equipo
        INNER JOIN jornada j ON j.id_jornada = ce.id_jornada
        INNER JOIN estado_conductor ec ON ec.id_estado_conductor = c.id_estado_conductor 
        INNER JOIN base b ON b.id_base = c.id_base
        WHERE b.nombre_base = 'Base' AND ec.abrev_estado_conductor != 'DES') A
group by A.sigla_equipo, A.patente_equipo

I am using your query, as an input to my query. That means, that the from of my query is not a table, but that it is the result of your query.

So, there's a from, which contains all your query, and that's what I call A, and that's what it takes to do the group by.

    
answered by 19.10.2018 / 17:17
source
2

You're on the right line with the idea of grouping. What you must do is, for the columns dia and noche apply some aggregation function, such as min() or max() . Since in the row of day the column noche is empty, and vice versa, it should work well with both.

The solution would be, for example:

SELECT   e.sigla_equipo
       , e.patente_equipo
       , max(IF(j.nombre_jornada = 'NOCHE', CONCAT(c.nombre_conductor, ' ',c.apellidoPat_conductor), '')) as noche
       , max(IF(j.nombre_jornada = 'DIA', CONCAT(c.nombre_conductor, ' ',c.apellidoPat_conductor), '')) as dia
  FROM conductor c
       INNER JOIN conductor_equipo ce ON ce.id_conductor = c.id_conductor
       INNER JOIN equipo e ON e.id_equipo = ce.id_equipo
       INNER JOIN jornada j ON j.id_jornada = ce.id_jornada
       INNER JOIN estado_conductor ec ON ec.id_estado_conductor = c.id_estado_conductor 
       INNER JOIN base b ON b.id_base = c.id_base
 WHERE b.nombre_base = 'Base' 
   AND ec.abrev_estado_conductor != 'DES'
 GROUP BY e.sigla_equipo
       , e.patente_equipo
    
answered by 19.10.2018 в 17:41