RE: Column Totals

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Because you don't technically 'need' it, unless you're trying to
reference the results by column name and you're using an aggregate like
that. :)

And in most systems, you don't need the "AS" just a space and a new
name:

SELECT SUM(Revenue) Revenue FROM Sales Where.....

But all depends on your style.  I don't think the SQL Server or Oracle
server I deal with care if you put the "AS" in there or not.  Again,
it's not required unless you're relying on a name coming back with the
result set, then you might want to be explict with an alias like that.

Also, when you use aggregates, remember that they'll they group the
non-aggregate functions based on uniqueness.  This will happen even if
you're not displaying the non-aggregate functions, so you may get
repeats of some things that you can get rid of by using SELECT DISTINCT
Sum(...

But if there's anything non-unique in what you're using in SELECT or
WHERE, it'll use that for grouping.  This includes the hours, minutes
and seconds in a date field sometimes.  If the query below worked for
you, then I guess it's enough for MySQL (is that what you're using?) but
in some systems it may not be and you may get every row in the database
non-SUM'd because the dates all have different "seconds" in the time.

In that case, you'd need to section out the day/month/year and compare
based on that or something.


Just some pitfalls to watch out for when you're not used to aggregates
and grouping.

-TG

> -----Original Message-----
> From: Ben Miller [mailto:benipmiller@xxxxxxxxxxx] 
> Sent: Monday, November 15, 2004 1:22 PM
> To: php-general@xxxxxxxxxxxxx
> Subject: RE:  Column Totals
> 
> 
> That did it perfectly.  Why don't any of the stupid books 
> tell you that you
> need the AS statement?
> 
> -----Original Message-----
> From: Jay Blanchard [mailto:jay.blanchard@xxxxxxxxxxxxxxxxxxxxx]
> Sent: Monday, November 15, 2004 11:16 AM
> To: benipmiller@xxxxxxxxxxx; php-general@xxxxxxxxxxxxx
> Subject: RE:  Column Totals
> 
> 
> [snip]
> The following query seems to return an empty string, which should only
> give
> me the total for the entire column anyway.
> $query = "SELECT SUM(Revenue) FROM Sales WHERE Date='$Date'";
> [/snip]
> 
> $query = "SELECT SUM(Revenue) AS Revenue FROM Sales WHERE 
> Date='$Date'";
> note the AS statement---------^
> 
> $foo = mysql_query($query, $connection);
> 
> $bar = mysql_fetch_array($foo);
> 
> echo $bar['Revenue']
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux