Using the following MySQL query, I created a pivot table that exactly matches what I'm looking for. However, I would like to replace the NULL values โโwith actual descriptions such as SubTotal and GrandTotal. Here is the pivot table form displayed on my PHP output (hopefully the formatting is somewhat legible!).
Name Division 1 Division 2 Division 3 Division 4 Location Name 1 Name 2 Name 3 NULL Total Total Total Total Name 4 Name 5 NULL Total etc NULL Column Grand Total
Here is the query that I used to create the table. After exploring this issue, it seems that the CASE function is the way to go. However, when I add two CASE lines to the query below, it does not seem to want to work. The returned mysql_error says that the GROUPING function does not exist.
SELECT CASE WHEN (GROUPING(name)=1) THEN 'MainTotal' ELSE name END AS name, CASE WHEN (GROUPING(location)=1) THEN 'SubTotal' ELSE location END AS location, name AS Name, SUM(IF(division='OEM',totalHours,NULL)) AS OEM, SUM(IF(division='A/M',totalHours,NULL)) AS AM, SUM(IF(division='SKF',totalHours,NULL)) AS SKF, SUM(IF(division='RE',totalHours,NULL)) AS RE, location as Location FROM $databasetable GROUP BY location, name WITH ROLLUP
Can someone tell me what I'm doing wrong? Is the CASE function a way to replace NULL category names?
Thanks in advance!
Andrew
source share