SELECT `cusid` AS Kund, SUM(
IF (
chargeratetype = "N", 1, 0
) ) AS Normal, SUM(
IF (
chargeratetype = "O", 1, 0
) ) AS Ovrig, SUM(
IF (
chargeratetype = "G", 1, 0
) ) AS Garanti, COUNT( * ) AS Total
FROM jobshistory
WHERE 1
GROUP BY `cusid`
ORDER BY `cusid`
The above query is going to select data from this table:
cusid chargeabletime chargeratetype
174000 1 N
370047 2 G
6806000 3 N
6806000 2 O
6806000 3 N
6806000 2 N
208700 2 N
174000 1 N
This gives the result something like this (not the real values):
Kund Normal Ovrig Garanti Total
174000 5 0 2 7
180200 3 1 1 5
184700 1 0 1 2
My question is: Why does the "Normal" column include all other columns? It doesnt only list results marked as "N".