MySQL: cuente filas según el nombre de los campos (Parte 2)

I have two mysql table:

Defectos

df_id | project_id | resp_id | status
------+------------+---------+----------
1     |     1      |    1    |  Open 
2     |     1      |    1    |  Open
3     |     1      |    1    |  Closed
4     |     1      |    2    |  Open
5     |     1      |    2    |  Closed

Responsable

resp_id | resp_comp_name
--------+----------------
   1    |     Google
   2    |     Firefox

and I require output

resp_comp_name | open | closed
---------------+------+--------
  Google       |  3   |    1
  Firefox      |  1   |    1

Escribí:

     SELECT r.resp_comp_name, d.status, COUNT(d.df_id) AS total
       FROM pms_defects d
  LEFT JOIN pms_responsibles r ON d.resp_id=r.resp_id
      WHERE d.project_id='1' AND d.resp_id != 0
   GROUP BY d.resp_id
   ORDER BY total DESC

produce:

enter image description here

preguntado el 02 de febrero de 12 a las 11:02

That's a good story. What would you like us to do? -

1 Respuestas

SELECT r.resp_comp_name, sum(if(d.status = 'Open',1,0)) as open, 
sum(if(d.status ='Closed',1,0)) as closed from pms_defects d, pms_responsibles r 
where r.resp_id = d.resp_id group by d.resp_id 

This uses if statements and sum to move open/closed from row- to column-based.

Respondido 02 Feb 12, 15:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.