sum total in a query with group by used

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

sum total in a query with group by used

Post by rsmarsha »

I have the following query:

Code: Select all

$ref2 = "SELECT DATE_FORMAT(dateadded, '%d-%m-%Y') AS dateadded,COUNT(refer) AS refno,refer,status FROM orders $filter $date GROUP BY refer ORDER BY refno DESC";
Is there a way to find the sum total of "refer" using the same query?

At present i'm pulling out all a count of "refers" grouped together in sets, but i also need an overall count of those found in the above query.
dreamline
Forum Contributor
Posts: 158
Joined: Fri May 28, 2004 2:37 am

Post by dreamline »

How about a mysql_num_rows($result) that gives you the total rows found. :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

$ref2 = "SELECT DATE_FORMAT(`dateadded`, '%d-%m-%Y') AS `dateadded`,COUNT(`refer`) AS `refno`, SUM(`refer`) as `sum`, refer,`status` FROM `orders` $filter $date GROUP BY `refer` ORDER BY `refno DESC`";
Post Reply