I think thats the right approach, but I couldn't get your code to work - probably because I'm on mysql 4.0 which doesnt support subqueries (should have mentioned that) but it did lead me to a similar approach of having a conditional statement to calculate a total for each column.
So there query generated would be as such:
SELECT prodCode, prodName,
SUM(IF(from_unixtime(uts,'%M') = 'January' ,prodQty,0)) as January,
SUM(IF(from_unixtime(uts,'%M') = 'February' ,prodQty,0)) as February,
SUM(IF(from_unixtime(uts,'%M') = 'March' ,prodQty,0)) as March,
SUM(IF(from_unixtime(uts,'%M') = 'April' ,prodQty,0)) as April,
SUM(IF(from_unixtime(uts,'%M') = 'May' ,prodQty,0)) as May,
SUM(IF(from_unixtime(uts,'%M') = 'June' ,prodQty,0)) as June,
SUM(IF(from_unixtime(uts,'%M') = 'July' ,prodQty,0)) as July,
SUM(IF(from_unixtime(uts,'%M') = 'August' ,prodQty,0)) as August,
SUM(IF(from_unixtime(uts,'%M') = 'September' ,prodQty,0)) as September,
SUM(IF(from_unixtime(uts,'%M') = 'October' ,prodQty,0)) as October,
SUM(prodQty) as tot
from tOrders o
INNER JOIN tOrderPOs pos ON o.orderID = pos.orderID
INNER JOIN tOrderProds op ON pos.purchaseOrder = op.purchaseOrder
INNER JOIN tProducts p ON op.productID = p.productID
WHERE
from_unixtime(uts,'%Y') = 2005 AND
(
from_unixtime(uts,'%M') = 'January' OR from_unixtime(uts,'%M') = 'February' OR
from_unixtime(uts,'%M') = 'March' OR from_unixtime(uts,'%M') = 'April' OR from_unixtime(uts,'%M') = 'May' OR from_unixtime(uts,'%M') = 'June' OR
from_unixtime(uts,'%M') = 'July' OR from_unixtime(uts,'%M') = 'August' OR
from_unixtime(uts,'%M') = 'September' OR from_unixtime(uts,'%M') = 'October' )
AND prodCode LIKE 'hy%' GROUP BY prodCode
And the php is shown below:
$prodcondition = "";
if($prodCompCode != "") $prodcondition = " AND prodCode LIKE '$prodCompCode%'";
if($prodCompType == "week") $dategrp = "%u";
else $dategrp = "%M";
$dategrp = "from_unixtime(uts,'$dategrp')";
$monthArray = array('January', 'February', 'March','April', 'May','June','July','August','September','October','November','December');
if($prodCompDate1 != "" OR $prodCompDate2 != "")
{
if($prodCompDate1 != "") $dateCondition[$monthArray[$prodCompDate1 -1]] = "from_unixtime(uts,'%M') = '". $monthArray[$prodCompDate1 -1]."' ";
if($prodCompDate2 != "") $dateCondition[$monthArray[$prodCompDate2 -1]] = "from_unixtime(uts,'%M') = '". $monthArray[$prodCompDate2 -1]."' ";
}
else
{
$currentMonth = date('m');
foreach($monthArray as $index => $thismonth) if($index< $currentMonth) $dateCondition[$thismonth] = "from_unixtime(uts,'%M') = '$thismonth' ";
}
$sql = "SELECT prodCode, prodName";
foreach($dateCondition as $colName => $nextDateCondition) $sql.= ", SUM(IF($nextDateCondition,prodQty,0)) as $colName";
$sql .= ", SUM(prodQty) as tot ";
$sql .= " from tOrders o INNER JOIN tOrderPOs pos ON o.orderID = pos.orderID
INNER JOIN tOrderProds op ON pos.purchaseOrder = op.purchaseOrder INNER JOIN tProducts p ON op.productID = p.productID WHERE from_unixtime(uts,'%Y') = 2005 AND (";
foreach($dateCondition as $colName => $nextDateCondition) $sql.= "$nextDateCondition OR ";
$sql = substr($sql,0,-4). ") ";
$sql .= $prodcondition;
$sql .= " GROUP BY prodCode";
It works a treat and is a million times faster than my previous approach of multiple queries.
Thanks a lot peeps!