I have two mysql table:
Defects
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
Responsible
resp_id | resp_comp_name
--------+----------------
1 | Google
2 | Firefox
and I require output
resp_comp_name | open | closed
---------------+------+--------
Google | 3 | 1
Firefox | 1 | 1
I wrote:
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
it produce:

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