I managed with this somewhat the query wanted just not the placement of fields in the right column... Also would be nice to have 0 values when no result exist for comp=value
<table>
<tr><td>ESPID</td><td>Good</td><td>Bad</td><td>Cancelled</td><td>DNR</td></tr>
<?php
$resall = $MySQLiconn->query("
SELECT report.espid, SUM(report.comp) AS total1
FROM report
Where Comp = 1
UNION
SELECT report.espid, SUM(report.comp) AS total2
FROM report
Where Comp = 2
UNION
SELECT report.espid, SUM(report.comp) AS total3
FROM report
Where Comp = 3
UNION
SELECT report.espid, SUM(report.comp) AS total4
FROM report
Where Comp = 4
GROUP BY report.espid
ORDER BY report.espid
");
while($rowallesp=$resall->fetch_array())
{
?>
<tr>
<td><?php echo $rowallesp['report.espid']; ?></td>
<td><?php echo $rowallesp['total1']; ?></td>
<td><?php echo $rowallesp['total2']; ?></td>
<td><?php echo $rowallesp['total3']; ?></td>
<td><?php echo $rowallesp['total4']; ?></td> </tr>
<?php
}
?>
</table>
Returns in a list not a column:
espid total1
1 1
1 2
2 2
3 1
2 1
Preferred
espid total1 total2 total3 total4
1 1 2 0 0
2 2 0 3 0
3 1 0 0 0