Re: Subtotal

From: Date: Wed, 19 Mar 2003 16:18:43 +0000
Subject: Re: Subtotal
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-140293@lists.php.net to get a copy of this message
SELECT *, SUM(units) subtotal FROM table GROUP BY date - do you mean this? Mike Smith wrote:
I'm trying to present a simple SELECT statement "GROUPED" by date. Ex.
   WO    UNITS    OEM    COMMENTS    DATE
   123     10            Me        Notes                03/18/2003
   456     5              You       More Notes      03/18/2003
Total       15
... I have it working grouping the dates together, but I can't seem to get the Total Units for a group. You'll see $body .= " instead of echo... because this is sent as an email. Here is the relevent code: <?php //table headers ... while($row = mssql_fetch_array($result)) { // $row[10] = SHIP DATE (What I'm grouping by) // $row[0] = Unit Qty (What I want to SUM, based on each "Group") If ($lastship!='') { // skip the first record If ($lastship!=$row[10]) { $body .= "<tr>\n"; $body .= "<td><strong>Total</strong>\n"; $body .= "</td>\n"; $body .= "<td align=center><strong>$total</strong>\n"; $body .= "</td>\n"; $body .= "<td colspan=5>\n"; $body .= "</td>\n"; $body .= "</tr>\n"; $totqty=0; $lastqty=0; } $total=0; } $totqty=$lastqty+$row[0]; $total = $totqty+$total; $lastship = $row[10]; $lastqty=$row[0]; $body .= "<tr>\n"; $body .= "<td align=\"center\">\n"; $body .= "$row[11]\n"; $body .= "</td>\n"; $body .= "<td align=\"center\">\n"; $body .= "$row[0]\n"; $body .= "</td>\n"; $body .= "<td align=\"center\">\n"; $body .= "$row[1]\n"; $body .= "</td>\n"; $body .= "<td>\n"; $body .= "$row[12]\n"; $body .= "</td>\n"; $body .= "<td align=\"center\">\n"; $body .= "$row[8]\n"; $body .= "</td>\n"; $body .= "<td align=\"center\">\n"; $body .= "$row[9]\n"; $body .= "</td>\n"; $body .= "<td align=\"center\">\n"; $body .= "$row[10]\n"; $body .= "</td>\n"; $body .= "</tr>\n"; } ... //close table, html ?> Any help is appreciated. Thanks, Mike Smith


« previous php.general (#140293) next »