I have a table with a couple of thousand rows: from this I need to extract a total for column WTE for each value in column band, including those where the total is 0. I also need each total to be in a column of its own, so that I can easily update a summary table.
The code I have at present returns the values from the relevant rows:
SELECT
IF(band="E",WTE,0) AS `Band6_WTE`
FROM `orthoptists` AS o
LEFT JOIN `instances` AS i
ON o.instance_FK = i.id
WHERE i.region = 14
But when I add SUM(), the return is incorrect (zero, when it should be several thousands):
SELECT
IF(band="E",SUM(WTE),0) AS `Band6_WTE`
FROM `orthoptists` AS o
LEFT JOIN `instances` AS i
ON o.instance_FK = i.id
WHERE i.region = 14
I have looked at http://en.wikibooks.org/wiki/MySQL/Pivot_table, but I do not understand how that approach should be applied to my problem.
What should I do?
you must sum if: